1.Statement的使用
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Scanner;
public class TestJDBCStatementDelete {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner scan=new Scanner(System.in);
System.out.println("请输入要删除的id:");
int id=scan.nextInt();
Connection con=null;
Statement stmt=null;
try {
Class.forName("com.mysql.jdbc.Driver");
con=DriverManager.getConnection("jdbc:mysql://localhost:3306/hr","root","root");
stmt=con.createStatement();
String sql="delete from emp where id="+id;
int res=stmt.executeUpdate(sql);
if(res!=0)
System.out.println("删除成功");
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
if(con!=null)
try {
con.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if(stmt!=null)
try {
stmt.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
2.PreparedStatement的使用
package com.oracle.test1;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Scanner;
public class TestJDBCPreparedStatementInsert {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner scan=new Scanner(System.in);
System.out.println("请输入要插入的姓名、工资和雇佣时间:");
String name=scan.next();
int sal=scan.nextInt();
String hiredate=scan.next();
Connection con=null;
PreparedStatement stmt=null;
try {
Class.forName("com.mysql.jdbc.Driver");
con=DriverManager.getConnection("jdbc:mysql://localhost:3306/hr","root","root");
String sql="insert into emp(name,sal,hiredate) "+"values (?,?,?)";
stmt=con.prepareStatement(sql);
stmt.setString(1,name);
stmt.setInt(2,sal);
stmt.setString(3,hiredate);
int res=stmt.executeUpdate();
if(res!=0)
System.out.println("插入成功");
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
if(con!=null)
try {
con.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if(stmt!=null)
try {
stmt.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
3.ResultSetMetaData和DatabaseMetaData的使用
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Types;
public class Test {
public static void main(String[] args) {
try {
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/hr", "root", "root");
String sql = "select * from emp";
PreparedStatement stmt = conn.prepareStatement(sql,ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_UPDATABLE);
ResultSet rs = stmt.executeQuery();
//rs.next();
//rs.deleteRow();
/*rs.last();
System.out.println(rs.getRow());
rs.beforeFirst();
while(rs.next()){
System.out.println(rs.getString("name"));
}*/
ResultSetMetaData rsmd = rs.getMetaData();
System.out.println("列数:"+rsmd.getColumnCount());
System.out.println("列名:"+rsmd.getColumnName(5));
System.out.println("列所在表名:"+rsmd.getTableName(4));
DatabaseMetaData dbmt=conn.getMetaData();
System.out.println("数据库名称:"+dbmt.getDatabaseProductName());
System.out.println("数据库版本:"+dbmt.getDatabaseProductVersion());
System.out.println("数据库主版本:"+dbmt.getDatabaseMajorVersion());
System.out.println("数据库次版本:"+dbmt.getDatabaseMinorVersion());
int type=rsmd.getColumnType(2);
if(type==Types.INTEGER){
System.out.println("列类型:int");
}else if(type==Types.VARCHAR){
System.out.println("列类型:varchar");
}
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}