jdbc的使用步骤
1、加载驱动类
2、建立连接
3、执行SQL
4、遍历结果集
5、释放连接
以下代码均为自习练习,连接自己的数据库,对表进行简单的查询,删除操作,其中还有PreparedStatement的一个小练习。代码如下:
@Test
public void testPreparedDelete(){
/*
* 写的不是太规范,没有关闭资源
*/
try {
Class.forName("com.mysql.jdbc.Driver");
Connection conn=DriverManager.getConnection(
"jdbc:mysql://localhost:3306/hibernate3","root","123");
String sql="delete from STUDENTS where STUDENT_ID=?";
PreparedStatement ps=conn.prepareStatement(sql);
ps.setInt(1, 3);
int num=ps.executeUpdate();
System.out.println(num);
} catch (Exception e) {
}
}
@Test
public void testNormalDelete(){
Connection conn=null;
java.sql.Statement stmt=null;
try {
Class.forName("com.mysql.jdbc.Driver");
conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/hibernate3","root","123");
stmt=conn.createStatement();
String sql="delete from students where student_id=3";
int num=stmt.executeUpdate(sql);
if(num>0){
System.out.println("删除成功");
}else{
System.out.println("error");
}
} catch (Exception e) {
e.printStackTrace();
}
}
@Test
public void testNormalSelect(){
Connection conn=null;
Statement stmt=null;
ResultSet rs=null;
try {
Class.forName("com.mysql.jdbc.Driver");
conn=DriverManager.getConnection(
"jdbc:mysql://127.0.0.1:3306/hibernate3","root","123");
String sql="select * from students";
stmt=(Statement) conn.createStatement();
rs=stmt.executeQuery(sql);
while(rs.next()){
String name=rs.getString("STUDENT_NAME");
int age=rs.getInt("STUDENT_AGE");
int id=rs.getInt("STUDENT_ID");
System.out.println("id="+id+",name="+name+",age="+age);
}
} catch (Exception e) {
e.printStackTrace();
}finally{
try {
if(conn!=null){
conn.close();
}
if(stmt!=null){
stmt.close();
}
if(rs!=null){
rs.close();
}
} catch (Exception e2) {
// TODO: handle exception
}
}
}
public void testPreparedDelete(){
/*
* 写的不是太规范,没有关闭资源
*/
try {
Class.forName("com.mysql.jdbc.Driver");
Connection conn=DriverManager.getConnection(
"jdbc:mysql://localhost:3306/hibernate3","root","123");
String sql="delete from STUDENTS where STUDENT_ID=?";
PreparedStatement ps=conn.prepareStatement(sql);
ps.setInt(1, 3);
int num=ps.executeUpdate();
System.out.println(num);
} catch (Exception e) {
}
}
@Test
public void testNormalDelete(){
Connection conn=null;
java.sql.Statement stmt=null;
try {
Class.forName("com.mysql.jdbc.Driver");
conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/hibernate3","root","123");
stmt=conn.createStatement();
String sql="delete from students where student_id=3";
int num=stmt.executeUpdate(sql);
if(num>0){
System.out.println("删除成功");
}else{
System.out.println("error");
}
} catch (Exception e) {
e.printStackTrace();
}
}
@Test
public void testNormalSelect(){
Connection conn=null;
Statement stmt=null;
ResultSet rs=null;
try {
Class.forName("com.mysql.jdbc.Driver");
conn=DriverManager.getConnection(
"jdbc:mysql://127.0.0.1:3306/hibernate3","root","123");
String sql="select * from students";
stmt=(Statement) conn.createStatement();
rs=stmt.executeQuery(sql);
while(rs.next()){
String name=rs.getString("STUDENT_NAME");
int age=rs.getInt("STUDENT_AGE");
int id=rs.getInt("STUDENT_ID");
System.out.println("id="+id+",name="+name+",age="+age);
}
} catch (Exception e) {
e.printStackTrace();
}finally{
try {
if(conn!=null){
conn.close();
}
if(stmt!=null){
stmt.close();
}
if(rs!=null){
rs.close();
}
} catch (Exception e2) {
// TODO: handle exception
}
}
}