Jdbc(Java DataBase Connectivity), 是一种JAVA数据库连接方法。
实现步骤
1.加载驱动(先引入mysql的jar包)
2.创建连接
3.编写sql语句
4.获得statement对象(用来接收sql语句)
5.执行sql语句获得结果
6.结果处理
7.关闭资源
具体代码
先创建一个tb_user表(id,username,password)
查询
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
| package com.JDBC;
import java.sql.*;
public class find { public static void main(String[] args) { try{ Class.forName("com.mysql.jdbc.Driver"); Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/train","root","123456"); System.out.println("创建连接成功"); String sql="select * from tb_user"; PreparedStatement statement = connection.prepareStatement(sql); ResultSet resultSet = statement.executeQuery(); while (resultSet.next()){ System.out.println(resultSet.getInt(1)); System.out.println(resultSet.getString(2)); System.out.println(resultSet.getString(3)); } DBUtil.closeAll(resultSet,statement,connection); }catch (Exception e){ System.out.println(e); } } }
|
添加
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
| package com.JDBC;
import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement;
public class add { public static void main(String[] args) { try{ Class.forName("com.mysql.jdbc.Driver"); Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/train","root","123456"); System.out.println("创建连接成功"); String sql="insert tb_user values(null,'whb','785')"; PreparedStatement statement = connection.prepareStatement(sql); statement.executeUpdate(); DBUtil.closeAll(null,statement,connection); }catch (Exception e){ System.out.println(e); }
} }
|
删除
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
| package com.JDBC;
import java.sql.*;
public class Delete { public static void main(String[] args) { try{ Class.forName("com.mysql.jdbc.Driver"); Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/train","root","123456"); System.out.println("创建连接成功"); String sql="delete from tb_user where id = 2"; PreparedStatement statement = connection.prepareStatement(sql); statement.executeUpdate();
DBUtil.closeAll(null,statement,connection); }catch (Exception e){ System.out.println(e); }
} }
|
修改
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
| package com.JDBC;
import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement;
public class Change { public static void main(String[] args) { try{ Class.forName("com.mysql.jdbc.Driver"); Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/train","root","123456"); System.out.println("创建连接成功"); String sql="UPDATE tb_user set password = 423 where id=1"; PreparedStatement statement = connection.prepareStatement(sql); statement.executeUpdate(); DBUtil.closeAll(null,statement,connection); }catch (Exception e){ System.out.println(e); }
} }
|
关闭方法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
| package com.JDBC;
import java.sql.*;
public class DBUtil {
public static void closeAll(ResultSet resultSet, Statement statement, Connection connection) throws SQLException { if (resultSet!=null){ resultSet.close(); } if (statement!=null){ statement.close(); } if (connection!=null){ connection.close(); }
} }
|
简单易学,谢谢观看!