JDBC学习笔记2

数据库数据更新操作statement接口下操作:【代码】

import java.sql.DriverManager;
import java.sql.SQLException;

public class LinkUpdate {
private String driver="oracle.jdbc.OracleDriver";
private String url="jdbc:oracle:thin:@localhost:1521:XE";
private String user="victor";
private String password="victor";
public void link(String name){
java.sql.Connection con=null;
java.sql.Statement sta=null;
java.sql.ResultSet res=null;
try {
Class.forName(driver);
con=DriverManager.getConnection(url, user, password);
sta=con.createStatement();
String sql="update info set name='"+name+"' where name='***'";
res=sta.executeQuery(sql);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
if(res!=null)
res.close();
if(sta!=null)
sta.close();
if(con!=null)
con.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public static void main(String[] args) {
LinkUpdate l= new LinkUpdate();
l.link("***");
}
}

数据库数据操作prepareStatement接口下:【代码】

import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Date;
public class DisLink {
private String driver="oracle.jdbc.OracleDriver";
private String url="jdbc:oracle:thin:@localhost:1521:XE";
private String user="victor";
private String password="victor";
//预编译的prepareStatement接口下插入语句【带参数】
/*public void link(int id,String name,int age,String phone,String gender){
java.sql.Connection con=null;
java.sql.PreparedStatement pre=null;
try {
Class.forName(driver);
con=DriverManager.getConnection(url, user, password);
System.out.println(con);
//创建预编译的prepareStatement
String sql="insert into info values("+id+",'"+name+"','"+age+"','"+phone+"','"+gender+"')";
pre=con.prepareStatement(sql);
pre.execute();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
if(pre!=null)
pre.close();
if(con!=null)
con.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}*/
//预编译的prepareStatement接口下插入语句,插入单个对象数据【异构】【使用占位符(不带参数)】
/*public void link1(){
java.sql.Connection con=null;
java.sql.PreparedStatement pre=null;
try {
Class.forName(driver);
con=DriverManager.getConnection(url, user, password);
String sql="insert into info values(?,?,?,?,?)";
pre=con.prepareStatement(sql);
pre.setInt(1, 4);
pre.setString(2,"babc4");
pre.setInt(3, 18);
pre.setString(4, "1563");
pre.setString(5, "male");
pre.execute();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
if(pre!=null)
pre.close();
if(con!=null)
con.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}*/
//预编译的prepareStatement接口下插入语句,插入批量对象数据【同构】【使用占位符(不带参数)】
/*public void link2(){
java.sql.Connection con=null;
java.sql.PreparedStatement pre=null;
try {
Class.forName(driver);
con=DriverManager.getConnection(url, user, password);
String sql="insert into info values(?,?,?,?,?)";
pre=con.prepareStatement(sql);
for(int i=1;i<=20;i++){
pre.setInt(1, i);
pre.setString(2,"abc"+i);
pre.setInt(3, 9+i);
pre.setString(4, "153");
pre.setString(5, "male");
pre.execute();
}
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
if(pre!=null)
pre.close();
if(con!=null)
con.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}*/
//预编译的prepareStatement接口下批量插入数据,手动提交事务【***回滚***】,输出程序运行时间,将SQL语句要插入的批量数据对象全部保存到缓存中进行条件判断操作
/*public void link3(){
java.sql.Connection con=null;
java.sql.PreparedStatement pre=null;
try {
Class.forName(driver);
con=DriverManager.getConnection(url, user, password);
con.setAutoCommit(false);
String sql="insert into info values(?,?,?,?,?)";
pre=con.prepareStatement(sql);
long start_date=System.currentTimeMillis();
for(int i=51;i<=100;i++){
pre.setInt(1, i);
pre.setString(2,"abc"+i);
pre.setInt(3, 30+i);
pre.setString(4, "15163");
pre.setString(5, "male");
//把前面的SQL语句全部保存到缓存中
pre.addBatch();
if(i%10==0){
pre.executeBatch();
con.commit();
}
}
pre.executeBatch();
con.commit();
long time_end=System.currentTimeMillis();
System.out.println(time_end-start_date);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
if(pre!=null)
pre.close();
if(con!=null)
con.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}*/
//预编译的prepareStatement接口下查询数据库数据【不带参数】
/* public void link4(){
java.sql.Connection con=null;
java.sql.PreparedStatement pre=null;
java.sql.ResultSet res=null;
try {
Class.forName(driver);
con=DriverManager.getConnection(url, user, password);
String sql="select * from info where id=1";
pre=con.prepareStatement(sql);
res=pre.executeQuery();
while(res.next()){
System.out.println("id="+res.getInt(1)+" ; "+
"name="+res.getString(2)+" ; "+
"age"+res.getInt(3)+" ; "+
"phone"+res.getString(4)+" ; "+
"gender="+res.getString(5));
}
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
if(res!=null)
res.close();
if(pre!=null)
pre.close();
if(con!=null)
con.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}*/
//预编译的prepareStatement接口下查询数据库数据【不带参数】
/*public void link5(int id){
java.sql.Connection con=null;
java.sql.PreparedStatement pre=null;
java.sql.ResultSet res=null;
try {
Class.forName(driver);
con=DriverManager.getConnection(url, user, password);
String sql="select * from info where id=?";
pre=con.prepareStatement(sql);
pre.setInt(1, id);
res=pre.executeQuery();
while(res.next()){
System.out.println("id="+res.getInt(1)+" ; "+
"name="+res.getString(2)+" ; "+
"age"+res.getInt(3)+" ; "+
"phone"+res.getString(4)+" ; "+
"gender="+res.getString(5));
}
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
if(pre!=null)
pre.close();
if(con!=null)
con.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}*/
//预编译的prepareStatement接口下插入语句【带参数】,【有占位符】
/*public void link6(int id,String name,int age,String phone,String gender){
java.sql.Connection con=null;
java.sql.PreparedStatement pre=null;
java.sql.ResultSet res=null;
try {
Class.forName(driver);
con=DriverManager.getConnection(url, user, password);
String sql="insert into info values(?,?,?,?,?)";
pre=con.prepareStatement(sql);
pre.setInt(1,id);
pre.setString(2,name);
pre.setInt(3,age);
pre.setString(4,phone);
pre.setString(5,gender);
pre.executeQuery();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
if(res!=null)
res.close();
if(pre!=null)
pre.close();
if(con!=null)
con.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}*/
//预编译的prepareStatement接口下插入语句[时间的处理方式](没传入参数)
public void link7(){
java.sql.Connection con=null;
java.sql.PreparedStatement pre=null;
java.sql.ResultSet res=null;
try {
Class.forName(driver);
con=DriverManager.getConnection(url, user, password);
String sql="insert into data values(?,?,?)";
pre=con.prepareStatement(sql);
pre.setInt(1, 1);
pre.setString(2, "****");
Date date=new Date();
//pre.setDate(3, new java.sql.Date(date.getTime()));
pre.setDate(3,new java.sql.Date(date.getTime()));
res=pre.executeQuery();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
if(res!=null)
res.close();
if(pre!=null)
pre.close();
if(con!=null)
con.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public static void main(String[] args) {
DisLink dis=new DisLink();
//dis.link(3, "***3", 17, "163", "male");
//dis.link1();
//dis.link2();
//dis.link3();
//dis.link4();
//dis.link6(103, ***103", 15, "1563", "male");
dis.link7();
}
}

上一篇:C# 扩展类


下一篇:[转载]linux下svn命令使用大全