连接数据库
1.加载驱动,固定写法
Class.forName("com.mysql.jdbc.Driver");
2.用户的信息和url
jdbc:mysql://主机地址:端口号/数据库名?参数1&参数2&参数3
useUnicode=true:支持中文编码;
characterEncoding=utf8:设定中文字符集为utf8
useSSL=false :不使用安全连接
String url = "jdbc:mysql://localhost:3306/wkx?useUnicode=true&characterEncoding=utf8&useSSL=false";
String username = "***";
String password = "***";
3.连接数据库 Connection 代表数据库
这就相当于在可视化界面输入用户密码连接数据库,使用DriverManager类
Connection connection = DriverManager.getConnection(url,username,password);
4. 创建一个执行sql的对象 statement
Statement statement = connection.createStatement();
statement.executeQuery(sql); // executeQuery:查询操作
int i = statement.executeUpdate(sql); //更新操作,返回一个整数值:受影响的行数
到这里就可以执行sql语句了,所有的sql语句都通过statement下的方法来执行
查询需要用resultset结果集来接收,封装了所有的查询结果
//getXxx获取指定类型数据
resultSet.getObject("id")
resultSet.getString("NAME")
//移动到下一个
resultSet.next()
5.最后需要关闭连接
resultSet.close();
statement.close();
connection.close();
连接并查询代码(最初级)
public class Jdbc {
static String driver;
static String url;
static String user;
static String psw;
static {
driver = "com.mysql.jdbc.Driver";
url = "jdbc:mysql://localhost:3306/qcby?useUnicode=true&characterEncoding=utf8&useSSL=false";
user = "root";
psw = "root";
}
public static void main(String[] args) throws ClassNotFoundException, SQLException {
//加载驱动
Class.forName(driver);
//连接---connection可以看成是数据库对象
Connection connection = DriverManager.getConnection(url, user, psw);
//创建可以执行sql的对象
Statement statement = connection.createStatement();
String sql = "select * from student";
ResultSet resultSet = statement.executeQuery(sql);
while (resultSet.next()){
System.out.println(resultSet.getObject("id"));
System.out.println(resultSet.getObject("name"));
System.out.println(resultSet.getObject("sex"));
}
resultSet.close();
statement.close();
connection.close();
}
}
提取工具类
首先把参数放进资源文件中,在src目录下:db.properties
driver=com.mysql.jdbc.Driver
url = jdbc:mysql://localhost:3306/qcby?useUnicode=true&characterEncoding=utf8&useSSL=false
username = ***
password = ***
然后创建一个工具类,主要作用是加载驱动和连接数据库最后释放资源
import java.io.IOException;
import java.io.InputStream;
import java.sql.*;
import java.util.Properties;
/**
* @author wangkx
*/
public class JdbcUtils {
private static String driver = null;
private static String url = null;
private static String username = null;
private static String password = null;
static {
try {
//将这个文件存到流里
InputStream in = JdbcUtils.class.getClassLoader().getResourceAsStream("db.properties");
//将流里的数据存到properties对象里
Properties properties = new Properties();
properties.load(in);
//从properties对象里将需要的数据取出
driver = properties.getProperty("driver");
url = properties.getProperty("url");
username = properties.getProperty("username");
password = properties.getProperty("password");
//加载驱动 只需要一次
Class.forName(driver);
} catch (IOException | ClassNotFoundException e) {
e.printStackTrace();
}
}
/**
* 获取数据库连接的方法
* @return
* @throws SQLException
*/
public static Connection getConnection() throws SQLException {
return DriverManager.getConnection(url, username, password);
}
/**
* 释放连接的方法
* @param cn
* @param st
* @param rs
*/
public static void release(Connection cn, Statement st, ResultSet rs){
if(rs != null){
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(st != null){
try {
st.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(cn != null){
try {
cn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
然后来测试增删改查,依然可以封装到方法中去,理解思想
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
/**
* @author wangkx
*/
public class JdbcOperation {
static Connection cn = null;
static Statement st = null;
static ResultSet rs = null;
/**
* 查询名字
* @param sql
*/
public static void select(String sql){
try {
cn = JdbcUtils.getConnection();
st = cn.createStatement();
rs = st.executeQuery(sql);
while (rs.next()){
System.out.println(rs.getObject("name"));
}
} catch (SQLException e) {
e.printStackTrace();
}finally {
JdbcUtils.release(cn,st,rs);
}
}
/**
* 更新记录
* @param sql
*/
public static void update(String sql){
try {
cn = JdbcUtils.getConnection();
st = cn.createStatement();
int i= st.executeUpdate(sql);
if(i>0) {
System.out.println("更新成功");
}
} catch (SQLException e) {
e.printStackTrace();
}finally {
JdbcUtils.release(cn,st,rs);
}
}
}
PreparedStatement
Statement对象会导致sql注入问题
PreparedStatement可以防止sql注入,效率会更高,
和statement的使用区别多了一个预编译过程,