JDBC开发步骤
1、注册驱动
2、获得连接
3、获得语句执行者
4、执行sql
5、处理结果
6、释放资源
1、注册驱动
方法1:建议使用代码:Class.forName(“com.mysql.jdbc.Driver”)
1、JDBC规范定义驱动接口:java.sql.Driver
2、MySQL驱动包提供了实现类:com.mysql.jdbc.Driver
方法2:DriverManager工具类,提供注册驱动的方法registerDriver(),方法的参数是java.sql.Driver,所以我们可以
通过如下语句注册DriverManager.registerDriver(new com.mysql.jdbc.Driver());
以上代码不推荐使用,存在2方面不足
1、硬编码,后期不易于程序扩展和维护
2、驱动被注册两次
通常我们开发使用Class.forName()加载一个使用字符串描述的驱动类。如果使用Class.forName()将类加载到内存。
该类的静态代码将自动执行,在查询com.mysql.jdbc.Driver源码,我们发现Driver类主动将自己进行注册了。
public class Driver extends NonRegisteringDriver implements java.sql.Driver {
//
// Register ourselves with the DriverManager
//
static {
try {
java.sql.DriverManager.registerDriver(new Driver());
} catch (SQLException E) {
throw new RuntimeException("Can't register driver!");
}
}
/**
* Construct a new driver and register it with DriverManager
*
* @throws SQLException
* if a database error occurs.
*/
public Driver() throws SQLException {
// Required for Class.forName().newInstance()
}
}
2、获取连接
Connection conn=DriverManager.getConnection(“jdbc:mysql://cdh1:3306/test”,”root”,”root”)
这个需要传入3个参数
1、url需要连接的数据库位置
2、用户名
3、密码
URL可以做一些扩展 jdbc:mysql://cdh1:3306/test?useUnicode=true&characterEncoding=UTF8
mysql使用的UTF8不是UTF-8
3、获得语句执行者
Statement stmt = conn.createStatement();
String sql=”select * from user where User=? and Password= ? ”
也可以是预处理
String sql=”select * from user where User=? and Password= ? “;
PreparedStatement pstat= conn.prepareStatement(sql);
4、执行sql
ResultSet rs= stat.executeQuery(sql);
预处理的方式ResultSet rs= pstat.executeQuery();
5、处理结果
rs.next()
6、释放资源
if (rs != null) rs.close();
if (pstat != null) pstat.close();
if (conn != null) conn.close();
测试代码
package xlucas.jdbc;
import org.junit.Test;
import java.sql.*;
/**
* Created by Xlucas on 2018/4/1.
*/
public class JdbcConnect {
public static void main(String[] args){
}
@Test
public void testLogin() {
try {
login("root' or 'root", "*6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9");
login1("root' or 'root", "*6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9");
} catch (Exception e) {
e.printStackTrace();
}
}
public void login(String username, String password) throws ClassNotFoundException, SQLException {
Class.forName("com.mysql.jdbc.Driver");
Connection conn= DriverManager.getConnection("jdbc:mysql://cdh1:3306/mysql","root","123456");
String sql="select * from user where "+"User='"+username+"'"+" and Password='"+password+"'";
Statement stat=conn.createStatement();
ResultSet rs= stat.executeQuery(sql);
if (rs.next()) {
System.out.println("恭喜您," + username + ",登录成功!");
} else {
System.out.println("账号或密码错误!");
}
if (rs != null)
rs.close();
if (stat != null)
stat.close();
if (conn != null)
conn.close();
}
public void login1(String username, String password) throws ClassNotFoundException, SQLException {
Class.forName("com.mysql.jdbc.Driver");
Connection conn= DriverManager.getConnection("jdbc:mysql://cdh1:3306/mysql","root","123456");
String sql="select * from user where User=? and Password= ? ";
PreparedStatement pstat= conn.prepareStatement(sql);
pstat.setString(1,username);
pstat.setString(2,password);
ResultSet rs= pstat.executeQuery();
if (rs.next()) {
System.out.println("恭喜您," + username + ",登录成功!");
} else {
System.out.println("账号或密码错误!");
}
if (rs != null)
rs.close();
if (pstat != null)
pstat.close();
if (conn != null)
conn.close();
}
}