一、使用JDBC的基本步骤
1.创建一个Java工程
2.根目录下面创建lib文件夹
3.引入相关jar包—导包
4.创建一个文件
5.注册驱动(可以不写)DriverManger
6.创建连接 DriverManger.getConnection()
7.创建通道 状态通道statement
8.写sql语句
9.进行增删改查
增、删、改:excuteUpdate() 返回int类型的数
查询:excuteQuery ResultSet next()
10. 遍历结果集
getInt() getString()
11.关闭资源
public static void main(String[] args) throws SQLException {
//1.注册驱动
DriverManager.registerDriver( new Driver());
//2.建立连接
Connection conn=DriverManager.getConnection("jdbc:mysql://localhost/shopping?useUnicode=true&characterEncoding=utf-8", "root", "123456");
//3.创建通道statement
Statement sta=conn.createStatement();
//4.执行SQL语句
/* String sql1="insert into product values(9,'华夫饼干',10,5)";//(1)增加
System.out.println("增加"+sta.executeUpdate(sql1)); */
String sql2="delete from product where pid=9";//(2)删除
System.out.println("删除"+sta.executeUpdate(sql2));
String sql3="update product set price=999 where pid=1";//(3)修改
System.out.println("修改"+sta.executeUpdate(sql3));
String sql4="select * from product";//(4)查询
ResultSet rs=sta.executeQuery(sql4);
//5.遍历结果集
while(rs.next()) {
System.out.println(rs.getObject(1)+" "+rs.getObject(2)+" "+rs.getObject(3)+" "+rs.getObject(4));
}
//6.关闭资源
conn.close();
sta.close();
}
二、工具类的构建
将对数据库的操作封装成工具类,以免每次操作数据库都去编写上面代码
1.释放资源整合
2.防止二次注册
3.使用properties配置文件
配置文件:jdbc.properties:
url=jdbc:mysql://localhost/shopping?useUnicode=true&characterEncoding=utf-8
username=root
password=123456
//工具类
public class JDBC_Util {
static String url=null;
static String username=null;
static String password=null;
//读取配置文件
static{
try {
Properties pro=new Properties();
InputStream in=JDBC_Util.class.getClassLoader().getResourceAsStream("jdbc.properties");
pro.load(in);
url=pro.getProperty("url");
username=pro.getProperty("username");
password=pro.getProperty("password");
} catch (IOException e) {
e.printStackTrace();
}
}
//获取连接的方法
public static Connection getConn() {
Connection conn=null;
try {
//1.注册驱动java.sql.DriverManager.registerDriver(new Driver())
//DriverManager.registerDriver(new Driver());
//Class.forName("com.mysql.jdbc.Driver");
//2.创建链接
conn=DriverManager.getConnection(url,username,password);
} catch (SQLException e) {
e.printStackTrace();
}
return conn;
}
//关闭连接的方法
private static void closeConn(Connection conn) {
try {
if (conn!=null) {
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
//关闭通道的方法
private static void closeSta(Statement sta) {
try {
if (sta!=null) {
sta.close();
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//关闭结果集的方法
private static void closeRs(ResultSet rs) {
try {
if (rs!=null) {
rs.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
//释放资源的方法
public static void close(Connection conn,Statement sta,ResultSet rs) {
closeConn(conn);
closeSta(sta);
closeRs(rs);
}
}
使用工具类完成对数据库的CRUD
public static void main(String[] args) {
Connection conn=null;
Statement sta=null;
ResultSet rs=null;
try {
//1.通过工具类获取连接
conn=JDBC_Util.getConn();
//3.创建通道
sta=conn.createStatement();
//4.查询语句
String sql="select * from product";
//5.执行查询executeQuery 返回结果集
rs=sta.executeQuery(sql);
//6.遍历结果集
while (rs.next()) {
//getInt(index)/getString(index)
//index表示载数据库中的第几个字段
int id=rs.getInt(1);
String name=rs.getString(2);
double price=rs.getDouble(3);
int cno=rs.getInt(4);
System.out.println(id+"-->"+name+"-->"+price+"-->"+cno);
}
} catch (SQLException e) {
e.printStackTrace();
}
//7.释放资源
JDBC_Util.close(conn, sta, rs);
}