JDBC连接数据库总共七步:
1,加载数据库驱动(如oracle,mysql...);
2,提供JDBC连接的URL,USER,PASSWORD;
3,创建数据库连接;
4,创建Statement;
5,执行SQL语句;
6,处理结果;
7,关闭JDBC资源。
下面以MYSQL数据库为例!
注:别忘记加这个包!mysql-connector-java-5.0.8-bin.jar
以下是代码:
工具类:JDBCUtil.java (提供连接、关闭连接释放JDBC资源)
package collection.utils;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class JDBCUtil {
static{
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
//获得连接
public static Connection getConnection(){
Connection conn = null;
String url = "jdbc:mysql://localhost:3306/dbtest?useUnicode=true&characterEncoding=utf8";
String user = "root";
String password = "root";
try {
conn = DriverManager.getConnection(url, user, password);
System.out.println("connection is successful!");
} catch (SQLException e) {
System.out.println("connection error!");
e.printStackTrace();
}
return conn;
}
//释放JDBC资源(关闭顺序与声明时的顺序相反)
public static void release(Connection conn,Statement state,ResultSet rs){
if(rs != null){
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(state != null){
try {
state.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(conn != null){
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
测试类:DBTest.java (对数据进行增、删、改、查)
package collection.db.mysql;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import org.junit.Test;
import collection.utils.JDBCUtil;
public class DBTest {
private static Connection conn;
private static Statement state;
// 1,添加数据
@Test
public void insert() {
conn = JDBCUtil.getConnection();
try {
String sql = "INSERT INTO users(id,username,age,job,address) VALUES(1,'花无缺',20,'武林大侠','云游天下,四海为家!')";
state = conn.createStatement();
int count = state.executeUpdate(sql);
System.out.println("Operation is successful!");
System.out.println("共插入了" + count + "条数据!");
//操作后释放资源
JDBCUtil.release(conn, state, null);
} catch (SQLException e) {
System.out.println("insert error!");
e.printStackTrace();
}
}
// 删除数据
@Test
public void delete() {
conn = JDBCUtil.getConnection();
try {
String sql = "DELETE FROM users WHERE username='花无缺1'";
state = conn.createStatement();
int count = state.executeUpdate(sql);
System.out.println("Operation is successful!");
System.out.println("共删除了" + count + "条数据!");
JDBCUtil.release(conn, state, null);
} catch (SQLException e) {
System.out.println("delete error!");
e.printStackTrace();
}
}
//修改数据
@Test
public void update(){
conn = JDBCUtil.getConnection();
try {
String sql = "UPDATE users SET job = '天下第一' WHERE id = '1'";
state = conn.createStatement();
int count = state.executeUpdate(sql);
System.out.println("Operation is successful!");
System.out.println("共修改了" + count + "条数据!");
JDBCUtil.release(conn, state, null);
} catch (SQLException e) {
System.out.println("update error!");
e.printStackTrace();
}
}
//查询数据
@Test
public void query(){
conn = JDBCUtil.getConnection();
try {
String sql = "SELECT * FROM users";
state = conn.createStatement();
ResultSet rs = state.executeQuery(sql);
System.out.println("Operation is successful!");
System.out.println("查询的数据结果如下:");
while(rs.next()){
int id = rs.getInt("id");
int age = rs.getInt("age");
String username = rs.getString("username");
String job = rs.getString("job");
String address = rs.getString("address");
System.out.println("工号:"+id+",姓名:"+username+",年龄:"+age+",职位:"+job+",地址:"+address);
}
JDBCUtil.release(conn, state, rs);
} catch (SQLException e) {
System.out.println("query error!");
e.printStackTrace();
}
}
}
阅读(6004) | 评论(0) | 转发(0) |