当前位置:Gxlcms > mysql > JDBC–MYSQLEclipse的连接以及简单的使用_MySQL

JDBC–MYSQLEclipse的连接以及简单的使用_MySQL

时间:2021-07-01 10:21:17 帮助过:5人阅读

关于JDBC、MYSQL的连接网上有了很多教程,但是感觉都不够详细。这次作业刚好要用到JDBC,所以参考网上的方法顺手写了下步骤,以供以后使用。

首先下载一个WAMP,附上网盘下载链接:

http://pan.baidu.com/s/1qW2Uzc4

WAMP的介绍这里就不说了吧,集成了Mysql数据库并且提供phpMyadmin,方便使用。

其次下载JDBC的驱动链接:

http://pan.baidu.com/s/1eQDylfO

最后的工具就是eclipse或者myeclipse这个网上搜下就可以,任意版本。Eclipse是免费的。

附:集成android开发环境ADT(ECLIPSE)

http://pan.baidu.com/s/1bn7yBNL

连接步骤如下:

1.首先打开phpMyadmin,创建数据库(可以手动也可以用SQL)

CREATE DATABASE test; //创建一个数据库

use test; //指定test为当前要操作的数据库

之后创建表:

CREATE TABLE user (nameVARCHAR(20),password VARCHAR(20));

//创建一个表user,设置两个字段。

插入数据:

INSERT INTO userVALUES('huzhiheng','123456'); //插入一条数据到表中

2.打开Eclipse,创建一个项目(my),

操作:右键项目点击my--->build Path--->add external Archiver...选择jdbc驱动,点击确定。

\

我的项目列表:

3.驱动已经导入,下面我们来写一个程序验证一下

import java.sql.*;
public class MysqlJdbc {
  public static void main(String args[]) {
    try {
      Class.forName("com.mysql.jdbc.Driver");     //加载MYSQL JDBC驱动程序   
      //Class.forName("org.gjt.mm.mysql.Driver");
     System.out.println("Success loading Mysql Driver!");
    }
    catch (Exception e) {
      System.out.print("Error loading Mysql Driver!");
      e.printStackTrace();
    }
    try {
      Connection connect = DriverManager.getConnection(
          "jdbc:mysql://localhost:3306/test","root","");
           //连接URL为   jdbc:mysql//服务器地址/数据库名  ,后面的2个参数分别是登陆用户名和密码,起始密码一般为空

      System.out.println("Success connect Mysql server!");
      Statement stmt = connect.createStatement();
      ResultSet rs = stmt.executeQuery("select * from user");
                                                              //user 为你表的名称
      while (rs.next()) {
        System.out.println(rs.getString("name"));
      }
    }
    catch (Exception e) {
      System.out.print("get data error!");
      e.printStackTrace();
    }
  }
}

可以看到输出结果:

Success loading Mysql Driver!

Success connect Mysql server!

huzhiheng

即连接成功。

至于JDBC的插入数据、查询数据、删除数据详细见如下代码:

Add:
	JDBC_Add(String name,String character,String introduce) throws SQLException{
		Connection conn = null;
		Statement st = null;
		String sql ="insert into game(gamename,gamecharacter,gameintroduce)" +
 				"VALUES('" + name + "','" + character + "','" + introduce +"')";
		conn = DriverManager.getConnection(DBURL,DBUSER,"");
		st = conn.createStatement();
		st.executeUpdate(sql);
		st.close();
		conn.close();
	}
DEL:
	JDBC_DEL(String name) throws SQLException{
		Connection conn = null;
		Statement st = null;
		String sql ="DELETE FROM game WHERE gamename ='" +name+"'"; //NAME是String类型所以应该加引号
 				
		conn = DriverManager.getConnection(DBURL,DBUSER,"");
		st = conn.createStatement();
		st.executeUpdate(sql);
		st.close();
		conn.close();	}
SEL:
	JDBC_SEL(String name,JLabel showname,JLabel character,JLabel introduce) throws SQLException {
		Connection conn = null;
		Statement st = null;
		ResultSet rs = null;
		String sql ="SELECT gamename,gamecharacter,gameintroduce from game WHERE gamename ='" + name + "'";
		conn = DriverManager.getConnection(DBURL,DBUSER,"");
		st = conn.createStatement();
		rs = st.executeQuery(sql);
		while(rs.next()){
		
		showname.setText(rs.getString("gamename"));
		character.setText(rs.getString("gamecharacter"));
		introduce.setText(rs.getString("gameintroduce"));
		}
			
		rs.close();
		st.close();
		conn.close();
	}
UPD:
public class JDBC_UPD {
		
		private static final String DBDRIVER = "org.gjt.mm.mysql.Driver";
		private static final String DBURL = "jdbc:mysql://localhost:3306/mysql";
		private static final String DBUSER = "root";
		
		JDBC_UPD(String name,String character,String introduce) throws SQLException{
			Connection conn = null;
			Statement st = null;
			String sql ="UPDATE game SET gamename = '" + name + "',gamecharacter='" + character +"',gameintroduce='"+ introduce + "' WHERE gamename ='" + name + "'"; //重写N次
			conn = DriverManager.getConnection(DBURL,DBUSER,"");
			st = conn.createStatement();
			st.executeUpdate(sql);
			st.close();
			conn.close();
		}

----------------------------------------------END-----------------------------------------------------

人气教程排行