java調用數據庫示例

package chapter23;


//function:calling database in java


import java.sql.*;


public class TestDB
{
private Connection con=null;//Connection to database
private Statement st=null;//Object to execute sql statements
private PreparedStatement pst=null;//Object to execute the sql atatement which has been conpiled.
private ResultSet rs=null;//Result set


public TestDB(){
//Brigde connection
try
{
string driver="sun.jdbc.odbc.JdbcOdbcDriver";//Declare the driver of brigde
String ds="jdbc:odbc:student";//student is the name of the data source
String user="login";
String login="123456";
Class.forName(driver);//Mont the driver program of database
con=DriverManager.getConnection(ds,user,login);
if(con!=null){
System.out.println("Connect to database successfully.");
}


}
catch (Exception e)
{
System.out.println("Connect to database failling."+e.toString());
}
}


//Query
public void query(String sql){
try
{
st=con.createStatement();
rs=st.executeQuery(sql);//Execute sql statement and return the result set
while(rs!=null&&rs.next()){
int stu_num=rs.getString("name");
String birthday=rs.getString("Birthday");
int math=rs.getInt("math");
int english=rs.getInt("english");
System.out.println("No.="+stu_num+"\t Name="+name+"\t Birthday="+birthday+"\t Math="+math+"\t English="+english+"\n");
}
}
catch (Exception e)
{
System.out.println("Error in query."+e.toString());
}
}


//Insert,delete and update
public void add_update_Del(String sql){
try
{
//Generate the Statement object
con.setAutoCommit(false);
st=con.createStatement();
int x=st.executeUpdate(sql);//Execute the operation
System.out.println("Operte successfully "+x);
con.commit();
}
catch (Exception e)
{
System.out.println("Error in revising the data "+e.toString());
}
}


//Insert with precompilling
public void preparedAdd(int num,String name,String birthday,int math,int english){
try
{
con.setAutoCommit(false);
String sql="insert into stu_info (stu_num,name,birthday,math,english)values(?,?,?,?,?)";
pst=con.prepareStatement(sql);//Generate the object to execute Sql statement
pst.setInt(1,num);
pst.setString(2,name);
pst.setString(3,birthday);
pst.setInt(4,math);
pst.setInt(5,english);
int x=pst.executeUpdate();
con.commit();
System.out.println("Operate successfully."+x);


}
catch (Exception e)
{
System.out.println("Precompiling fails."+e.toString());
}
}
public void transaction(){
try
{
//Close the mode of autocommitment
con.setAutoCommit(false);
st=con.createStatement();
//these below is the math grade of the No.2 student and add it to 10
String sql1="select math from stu_info where stu_num=2";
rs=st.executeQuery(sql1);
rs.next();
int math1=rs.getInt(1)+5;
System.out.println("The updated grade of the No.2 student is:"+meth1);
//these below is the math grade of the No.3 student and add it to 10
String sql1="select math from stu_info where stu_num=3";
rs=st.executeQuery(sql2);
rs.next();
int math2=rs.getInt("math")+5;
System.out.println("The updated grade of the No.3 student is:"+meth2);
//the updated math grade of the two students
String sql3="update stu_info set math="+math+"where stu_num=2";
st.executeUpdate(sql3);//Execute the operation
String sql4="update stu_info set math="+math+"where stu_num=3";
st.executeUpdate(sql4);//Execute the operation
con.commit();
System.out.println("Submit successfully.");
}
catch (Exception e)
{
try
{
con.rollback();
System.out.println("Event rollback.");


}
catch (SQLException ignored)
{
}
}
}
//close
public void close(){
try
{
if(con!=null){
con.close();
}
}
catch (Exception e)
{
System.out.println("Exception when closing the database.");
}
}
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章