Java

本类阅读TOP10

·使用MyEclipse开发Struts框架的Hello World!(录像1)
·hibernate配置笔记
·AOP编程入门--Java篇
·linux下Tomcat 5.0.20 与 Apache 2 安装/集成/配置
·在win2003下整合了整合Tomcat5.5+ apache_2.0.53+ mod_jk_2.0.47.dll
·构建Linux下IDE环境--Eclipse篇
·Jsp 连接 mySQL、Oracle 数据库备忘(Windows平台)
·ASP、JSP、PHP 三种技术比较
·Tomcat5.5.9的安装配置
·AWT GUI 设计笔记(二)

分类导航
VC语言Delphi
VB语言ASP
PerlJava
Script数据库
其他语言游戏开发
文件格式网站制作
软件工程.NET开发
关于JDBC用法的练习

作者:未知 来源:月光软件站 加入时间:2005-2-28 月光软件站

/**

已安装了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;
}
/*
*/
}




相关文章

相关软件