JDBC概念:Java DataBase Connectivity Java 数据库连接, Java语言操作数据库
其实是官方(sun公司)定义的一套操作所有关系型数据库的规则,即接口。各个数据库厂商去实现这套接口,提供数据库驱动jar包。我们可以使用这套接口(JDBC)编程,真正执行的代码是驱动jar包中的实现类。
使用步骤:
1. 导入驱动jar包 mysql-connector-java-5.1.37-bin.jar
1.复制mysql-connector-java-5.1.37-bin.jar到项目的libs目录下
2.右键-->Add As Library
2. 注册驱动
3. 获取数据库连接对象 Connection
4. 定义sql
5. 获取执行sql语句的对象 Statement
6. 执行sql,接受返回结果
7. 处理结果
8. 释放资源
Connection connection=null;
PreparedStatement statement=null;
try {
//注册驱动
Class.forName("com.mysql.jdbc.Driver");
//获取数据库连接对象
connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/DB1","root","root");
//定义sql
String sql="select * from user ";
//执行sql 接收返回结果--statement对象
statement = connection.prepareStatement(sql);
//获取查询结果集resultSet
ResultSet resultSet = statement.executeQuery(sql);
//拿到结果集的数据
while(resultSet.next()){
int id=resultSet.getInt(1);//可传列数
System.out.println("id:"+id);
String name = resultSet.getString(2);
System.out.println("username:"+name);
String password = resultSet.getString("password");//也可直接传列名
System.out.println("password:"+password);
}
} catch (ClassNotFoundException | SQLException e) {
e.printStackTrace();
}
finally {
if(statement!=null){
try {
statement.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(connection!=null){
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
JDBCUtils工具类
配置文件:
url=jdbc:mysql://localhost:3306/DB1
user=root
password=root
driver=com.mysql.jdbc.Driver
public class JDBCUtils {
//JDBC工具类 直接获取连接对象,数据库url直接写在配置文件里面
private static String url;
private static String user;
private static String password;
private static String driver;
//使用静态代码块
static {
try {
//读取配置文件
Properties pro=new Properties();
URL resource = JDBCUtils.class.getClassLoader().getResource("jdbc.properties");
String path = resource.getPath();
System.out.println(path);
InputStream is = JDBCUtils.class.getClassLoader().getResourceAsStream("jdbc.properties");
pro.load(is);
url=pro.getProperty("url");
user= pro.getProperty("user");
password= pro.getProperty("password");
driver=pro.getProperty("driver");
Class.forName(driver);//注册驱动
} catch (IOException | ClassNotFoundException e) {
e.printStackTrace();
}
}
//获取连接对象connection
public static Connection getConnection() throws SQLException {
return DriverManager.getConnection(url,user,password);
}
//释放资源
public static void Close(Statement statement,Connection connection){
if(statement!=null){
try {
statement.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(connection!=null){
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
测试:
//使用工具类获取连接对象
Connection coon=JDBCUtils.getConnection();
//定义Sql语句
String sql="select * from user where username =? and password = ?";
//获取执行对象(防注入)
PreparedStatement preparedStatement = coon.prepareStatement(sql);
Scanner in = new Scanner(System.in);
System.out.println("请输入用户名");
String username=in.next();
System.out.println("请输入密码");
String password=in.next();
//传参数(?的位置,传入的数据)
preparedStatement.setString(1,username);
preparedStatement.setString(2,password);
ResultSet set = preparedStatement.executeQuery();
if(set.next()){
System.out.println("登录成功!");
}
else System.out.println("登陆失败!");
//释放资源
in.close();
JDBCUtils.Close(preparedStatement,coon);
数据库user表