关于JDBC用法的练习

类别:Java 点击:0 评论:0 推荐:

/**

已安装了microsoft 驱动程序

jdk1.4; win2000 server;  MS sql2000;

*/

import java.sql.*;


class sql
{
 
public static void main(String[] agrs) 
{
Connection  cn=null;
Statement stmt=null;
String     sql=null; 

try
 {
 Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
 }
catch(ClassNotFoundException ex)
 {
 System.out.println("Not find  the  Driver!");
 }

try

String urls="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=webroot";//webroot 库名.TALBE 是表名;
String user="sa";
String password="password";
cn= DriverManager.getConnection(urls,user,password);

//stmt=cn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
stmt=cn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_UPDATABLE);

sql="select  top 10 * from  TABLE1";
ResultSet rs= stmt.executeQuery(sql);
while(rs.next())
{
 System.out.println(rs.getString(2)+"       "+rs.getString(3));

 }

 rs.first();
 System.out.print(rs.getRow()+"       ");
 System.out.println(rs.getString(2)+" 1      "+rs.getString(3));

 rs.last();
 System.out.print(rs.getRow()+"       ");
 System.out.println(rs.getString(2)+" 2      "+rs.getString(3));

 rs.previous();
 System.out.print(rs.getRow()+"       ");
 System.out.println(rs.getString(2)+" 3      "+rs.getString(3));

 rs.next();
 System.out.print(rs.getRow()+"       ");
 System.out.println(rs.getString(2)+" 4      "+rs.getString(3));

 rs.absolute(2);
 System.out.print(rs.getRow()+"       ");
 System.out.println(rs.getString(2)+" 5      "+rs.getString(3));
 
 /*
 rs.afterLast();
 System.out.print(rs.getRow()+"       ");
 System.out.println(rs.getString(2)+"       "+rs.getString(3));
 System.out.print(rs.isAfterLast());

 rs.beforeFirst();
 System.out.print(rs.getRow()+"       ");
 System.out.println(rs.getString(2)+"       "+rs.getString(3));
 */

 String sql1="update  TABLE1 set 题目=?   where id=? ";
 PreparedStatement stmt1 = cn.prepareStatement(sql1);
 String stat  = new String("盛夏话足部保健");
 String stat1 = UnicodeToGB(stat);//解决中文问题
 stmt1.setString(1,stat1);
 stmt1.setInt(2,3423);
 stmt1.executeUpdate();
 cn.commit();

 //System.out.println(stat1);
 //System.exit(0);

 //cn.setAutoCommit(false);
 
 stmt.addBatch("update  TABLE1 set 题目='盛夏话足部保健1'   where id='3407'");
 stmt.addBatch("update  TABLE1 set 题目='夏季预防中暑膳食1' where id='3408'");
 stmt.addBatch("INSERT INTO  TABLE1  VALUES('11','12','13','','')");
 stmt.addBatch("INSERT INTO  TABLE1  VALUES('12','12','13','','')");
 stmt.addBatch("INSERT INTO  TABLE1  VALUES('13','12','13','','')");
 stmt.addBatch("INSERT INTO  TABLE1  VALUES('14','12','13','','')");
 stmt.addBatch("INSERT INTO  TABLE1  VALUES('15','12','13','','')");
 stmt.addBatch("INSERT INTO  TABLE1  VALUES('16','12','13','','')");
 stmt.addBatch("INSERT INTO  TABLE1  VALUES('17','12','13','','')");
 stmt.addBatch("INSERT INTO  TABLE1  VALUES('18','12','13','','')");
 
 int [] updateCounts=stmt.executeBatch();
 cn.commit();
 
 

stmt.close();
cn.close();
}
catch(SQLException e)
 {
 System.out.println("The SQLException error!");
 }

}
/*
*/

public static String UnicodeToGB(String strIn){
   byte[] b;
   String strOut = null;
   if(strIn == null || (strIn.trim()).equals(""))
   return strIn;
   try{
       b = strIn.getBytes("GBK");
       strOut = new String(b,"ISO8859_1");
   }
   catch(Exception e){
       System.out.println("unicodeToGB exception : " + e.getMessage() + "\n");
   }
   return strOut;
}
/*
*/
}

本文地址:http://com.8s8s.com/it/it18227.htm