Java数据库连接,Java语言操作数据库
//1. 导入驱动jar包
//2.注册驱动
Class.forName("com.mysql.jdbc.Driver");
//3.获取数据库连接对象
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/db3", "root", "root");
//4.定义sql语句
String sql = "update account set balance = 500 where id = 1";
//5.获取执行sql的对象 Statement
Statement stmt = conn.createStatement();
//6.执行sql
int count = stmt.executeUpdate(sql);
//7.处理结果
System.out.println(count);
//8.释放资源
stmt.close();
conn.close();
DriverManager:驱动管理对象
1.注册驱动:告诉程序使用哪一个数据库驱动jar包
Class.forName("com.mysql.jdbc.Driver");
注意:mysql 5 之后的驱动jar包可以省略此步骤
2.获取数据库连接:
static Connection getConnection(String url,String user,String password)
参数:
Connection:数据库连接对象
Statement:执行sql的对象
Statement stmt = null;
Connection conn = null;
try {
//1. 注册驱动
Class.forName("com.mysql.jdbc.Driver");
//2. 定义sql
String sql = "insert into account values(null,'王五',3000)";
//3.获取Connection对象
conn = DriverManager.getConnection("jdbc:mysql:///db3", "root", "root");
//4.获取执行sql的对象 Statement
stmt = conn.createStatement();
//5.执行sql
int count = stmt.executeUpdate(sql);//影响的行数
//6.处理结果
System.out.println(count);
if(count > 0){
System.out.println("添加成功!");
}else{
System.out.println("添加失败!");
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}finally {
//stmt.close();
//7. 释放资源
//避免空指针异常
if(stmt != null){
try {
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(conn != null){
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
ResultSet:结果及对象,封装查询结果
//循环判断游标是否是最后一行末尾。
while(rs.next()){
//获取数据
//6.2 获取数据
int id = rs.getInt(1);
String name = rs.getString("name");
double balance = rs.getDouble(3);
System.out.println(id + "---" + name + "---" + balance);
}
PreparedStatement:执行sql对象
public class JDBCUtils {
private static String url;
private static String user;
private static String password;
private static String driver;
//文件只需要读取一次,使用静态代码块
static{
//读取资源文件,获取值
try {
//1.创建properties集合类
Properties pro = new Properties();
//用ClassLoader类加载器获取src路径下文件
ClassLoader classLoader=JDBCUtils.class.getClassLoader();
URL res = classLoader.getResource("jdbc.properties");
String path = res.getPath();
sout(path);//绝对路径
//2. 加载文件
pro.load(new FileReader(path))
//3. 获取数据,赋值
url = pro.getProperty("url");
user = pro.getProperty("user");
password = pro.getProperty("password");
driver = pro.getProperty("driver");
//4. 注册驱动
Class.forName(driver);
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
//获取连接对象方法
public static Connection getConnection() throws SQLExection {
return DriverManager.getConnection(url,user,password);
}
//释放资源
public static void close(Statement stmt,Connection conn) {
}
public static void close(ResultSet rs,Statement stmt,Connection conn) {
if(rs!=null) {
try{
rs.close();
}catch (SQLException e) {
e.printStackTrance();
}
}
if( stmt != null){
try {
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if( conn != null){
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
一个包含多个步骤的业务操作。如果这个业务操作被事务管理,则这多个步骤要么同时成功,要么同时失败。
public class JDBCDemo {
public static void main(String[] args) {
Connection conn = null;
PreparedStatement pstmt1 = null;
PreparedStatement pstmt2 = null;
try {
//1. 获取连接
conn = JDBCUtils.getConnection();
//开启事务
conn.setAutoCommit(false);
//2. 定义sql
String sql1 = "update account set balance = balance - ? where id = ?";
String sql2 ="update account set balance + ? where id = ?";
//3. 获取执行sql对象
pstmt1 = conn.preparedStatement(sql1);
pstmt2 = conn.preparedStatement(sql2);
//4. 设置参数
pstmt1.setDouble(1,500);
pstmt1.setInt(2,1);
pstmt2.setDouble(1,500);
pstmt2.setInt(2,2);
//5. 执行sql语句
pstmt1.executeUpdate();
//制造异常
int i = 3/0;
Pstmt2.executeUpdate();
conn.commit();
} catch (Exection e) {
//事务回滚
try {
if(conn!=null) {
conn.rollback();
}
} catch (SQLException e1) {
e1.printStackTrace();
}
e.printStackTrace();
}finally {
JDBCUtils.close(pstmt1,conn);
JDBCUtils.close(pstmt2,null);
}
}
}
本文地址:https://blog.csdn.net/u014551318/article/details/85944038
如对本文有疑问, 点击进行留言回复!!
网友评论