JDBC连接MySQL数据库的流程
第一步:导入jar驱动包
1.鼠标放在工程目录下的src目录上,点击右键,选择Build Path
2.在Build Path下选择configure Build Path...
3.点击选择Libraries,并在右边选择Add External JARs...
4.找到提前准备好的jdbc的jar包并点击打开
5.点击OK
第二步:动态加载驱动
在main函数内输入 Class.forName("com.mysql.jdbc.Driver");
有异常则抛出异常或者室友try/catch捕获异常
第三步:创建数据库连接
Connection conn = DriverManager.getConnection(url,user,password)
url : 需要进行操作的数据库的详细路径,格式为:jdbc:mysql://127.0.0.1:3306/数据库名
user : 数据库的用户名 ,一般为root;
password : 数据库的连接密码
第四步: 对数据库进行操作(分为 PreparedStatement 和 Statement 其中Statement中可能出现漏洞,不建议使用)
1.Statement源码
数据库名:mySql
表名 :person
字段 :id (int) name(String) age (int) sex(String)
public static void main (String[] args) throws Exception{
//动态加载驱动
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://127.0.0.1:3306/mySql";
String user = "root";
String password = "123456";
//创建数据库连接
Connection conn = DriverManager.getConnection(url,user,password)
Statement stmt = conn.createStatement();
/*//增加数据 (运行时只要输出有结果即为成功,可能运行成功结果输出的是false)
String sql = "insert into person(id,name,age,sex) values(1001,'张三',20,'男')";
boolean bol = stmt.execute(sql);
System.out.println(bol);*/
/*//删除数据
String sql = "delete from person where name = '张三'";
boolean bol = stmt.execute(sql);
System.out.println(bol);*/
/*//修改数据
String sql = "update person set id =1002, name = '李四',age = 22,sex = '男' where id = 1003 ";
boolean bol = stmt.execute(sql);
System.out.println(bol);*/
/*//查询数据
String sql = "select * from person";
ResultSet rs = stmt.executeQuery(sql);
while(rs.next()){
int id = rs.getInt("id");
String name = rs.getString("name");
int age = rs.getInt("age");
String sex = rs.getString("sex");
System.out.println(id + "," + name + "," + age + "sex");
}*/
rs.close();
stmt.close();
conn.close();
}
2. PreparedStatement源码
数据库名:mySql
表名 :person
字段 :id (int) name(String) age (int) sex(String)
public static void main (String[] args) throws Exception{
Scanner sc = new Scanner(System.in);
//动态加载驱动
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://127.0.0.1:3306/mySql";
String user = "root";
String password = "123456";
//创建数据库连接
Connection conn = DriverManager.getConnection(url,user,password)
/*//增加数据 (运行时只要输出有结果即为成功,可能运行成功结果输出的是false)
System.out.println("请输入id,姓名,年龄,性别");
int id = sc.nextInt();
String name = sc,next();
int age = sc.nextInt();
String sex = sc,next();
String sql = "insert into person(id,name,age,sex) values(?,?,?,?)";
PreparedStatement ps = conn.prepareStatement(sql);
ps.setInt(1,id);
ps.setString(2,name);
ps.setInt(3,age);
ps.setString(4,sex);
int i = ps.executeUpdate();
System.out.println(i);*/
/*//删除数据
System.out.println("请输入要删除数据的姓名:");
String name = sc.next();
String sql = "delete from person where name = ?";
PreparedStatement ps = conn.prepareStatement(sql);
ps.setString(1,name);
int i = ps.executeUpdate();
System.out.println(i);*/
/*//修改数据
System.out.println("请输入id,姓名,年龄,性别");
int id = sc.nextInt();
String name = sc,next();
int age = sc.nextInt();
String sex = sc,next();
String sql = "update person set id =?, name = ?,age = ?,sex = ? where id = 1003 ";
PreparedStatement ps = conn.prepareStatement(sql);
ps.setInt(1,id);
ps.setString(2,name);
ps.setInt(3,age);
ps.setString(4,sex);
int i = ps.executeUpdate();
System.out.println(i);*/
/*//查询数据
String sql = "select * from person";
PreparedStatement ps = conn.prepareStatement(sql);
while(rs.next()){
int id = rs.getInt("id");
String name = rs.getString("name");
int age = rs.getInt("age");
String sex = rs.getString("sex");
System.out.println(id + "," + name + "," + age + "sex");
}*/
/*//关闭连接
ps.close();
conn.close();*/
}