package com.yanyu;
import java.sql.*;
import java.util.ResourceBundle;
public class JDBCTest01 {
public static void main(String[] args) {
ResourceBundle bundle = ResourceBundle.getBundle("com/resources/db");
String driver = bundle.getString("driver");
String url = bundle.getString("url");
String user = bundle.getString("user");
String password = bundle.getString("password");
Connection con = null;
Statement st = null;
ResultSet rs = null;
try {
Class.forName(driver);
con = DriverManager.getConnection(url,user,password);
con.setAutoCommit(false);
String sql = "insert into user values(1,'yy')";
st = con.createStatement();
st.execute(sql);
con.commit();
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
} catch (SQLException e) {
if (con != null) {
try {
con.rollback();
} catch (SQLException ex) {
throw new RuntimeException(ex);
}
}
throw new RuntimeException(e);
}finally {
if (rs != null) {
try {
rs.close();
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
if (st != null) {
try {
st.close();
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
if (con != null) {
try {
con.close();
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
}
}
}
package com.yanyu;
import java.sql.*;
import java.util.ResourceBundle;
public class JDBCTest02 {
public static void main(String[] args) {
ResourceBundle bundle = ResourceBundle.getBundle("com/resources/db");
String driver = bundle.getString("driver");
String url = bundle.getString("url");
String user = bundle.getString("user");
String password = bundle.getString("password");
Connection con = null;
PreparedStatement ps = null;
ResultSet rs = null;
try {
Class.forName(driver);
con = DriverManager.getConnection(url,user,password);
con.setAutoCommit(false);
String sql = "update user set name = ? where id = ?";
ps = con.prepareStatement(sql);
ps.setInt(2,1);
ps.setString(1,"yanyu");
ps.execute();
con.commit();
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
} catch (SQLException e) {
if (con != null) {
try {
con.rollback();
} catch (SQLException ex) {
throw new RuntimeException(ex);
}
}
throw new RuntimeException(e);
}finally {
if (rs != null) {
try {
rs.close();
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
if (ps != null) {
try {
ps.close();
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
if (con != null) {
try {
con.close();
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
}
}
}
package com.yanyu;
import java.sql.*;
import java.util.ResourceBundle;
public class JDBCTest03 {
public static void main(String[] args) {
ResourceBundle bundle = ResourceBundle.getBundle("com/resources/db");
String driver = bundle.getString("driver");
String url = bundle.getString("url");
String user = bundle.getString("user");
String password = bundle.getString("password");
Connection con = null;
PreparedStatement ps = null;
ResultSet rs = null;
try {
Class.forName(driver);
con = DriverManager.getConnection(url,user,password);
con.setAutoCommit(false);
String sql = "delete from user where id = ?";
ps = con.prepareStatement(sql);
ps.setInt(1,3);
ps.execute();
con.commit();
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
} catch (SQLException e) {
if (con != null) {
try {
con.rollback();
} catch (SQLException ex) {
throw new RuntimeException(ex);
}
}
throw new RuntimeException(e);
}
}
}
package com.yanyu;
import java.sql.*;
import java.util.ResourceBundle;
public class JDBCTest04 {
public static void main(String[] args) {
ResourceBundle bundle = ResourceBundle.getBundle("com/resources/db");
String driver = bundle.getString("driver");
String url = bundle.getString("url");
String user = bundle.getString("user");
String password = bundle.getString("password");
Connection con = null;
PreparedStatement ps = null;
ResultSet rs = null;
try {
Class.forName(driver);
con = DriverManager.getConnection(url,user,password);
con.setAutoCommit(false);
String sql = "select * from user";
ps = con.prepareStatement(sql);
rs = ps.executeQuery();
while (rs.next()){
String name = rs.getString("name");
String id = rs.getString("id");
System.out.println(id + "---->" + name);
}
con.commit();
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
} catch (SQLException e) {
if (con != null) {
try {
con.rollback();
} catch (SQLException ex) {
throw new RuntimeException(ex);
}
}
throw new RuntimeException(e);
}finally {
if (rs != null) {
try {
rs.close();
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
}
}
}