tablesql.java
来自「《移动Agent技术》一书的所有章节源代码。」· Java 代码 · 共 338 行
JAVA
338 行
package shezhi;
import java.sql.*;
import java.io.*;
import java.net.*;
import java.util.Date;
import java.util.Properties;
public class tableSql{
ResultSet rs;
Connection con;
Statement stmt;
Statement oldStmt;
public void tableInit(String data_source){
try{
DatabaseMetaData dma;
// Class.forName("com.ashna.jturbo.driver.Driver");
// con = DriverManager.getConnection("jdbc:JTurbo://localhost:1433/"+data_source+"/charset=gb2312", "sa", "");
// Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
// con=DriverManager.getConnection("jdbc:odbc:"+data_source,"sa","");
// checkForWarning(con.getWarnings());
Properties prop = new Properties();
prop.put("user", "sa");
prop.put("password", "");
prop.put("server", "localhost");
prop.put("port", "1433");
prop.put("db", "cqjk");
prop.put("charset", "GB2312");
Class.forName("weblogic.jdbc.mssqlserver4.Driver");
con = DriverManager.getConnection("jdbc:weblogic:mssqlserver4", prop);
//
dma=con.getMetaData();
System.out.println("\nConnected to "+dma.getURL());
System.out.println("Driver "+dma.getDriverName());
System.out.println("Version "+dma.getDriverVersion());
System.out.println("");
}
catch(ClassNotFoundException e){
System.out.println("ClassNotFoundExeption");
}
catch(SQLException ex){
System.out.println("\n***SQLException caught ***\n");
while (ex != null){
System.out.println("SQLState:"+ex.getSQLState());
System.out.println("Message:"+ex.getMessage());
System.out.println("Vendor:"+ex.getErrorCode());
ex=ex.getNextException();
System.out.println("");
}
}
}
////checkFor Warning
/* private static boolean checkForWarning(SQLWarning warn) throws SQLException{
boolean rc=false;
if (warn != null){
System.out.println("\n***Warning***\n");
rc=true;
while(warn != null){
System.out.println("SQLState:"+warn.getSQLState());
System.out.println("Message:"+warn.getMessage());
System.out.println("Vendor:"+warn.getErrorCode());
System.out.println("");
warn=warn.getNextWarning();
}
}
return rc;
}
*/
////oper query
public ResultSet recordSql(String sql){
try{
stmt=con.createStatement();
rs=stmt.executeQuery(sql);
}
catch(SQLException ex){
System.out.println("\n***SQLException caught22222 ***\n");
while (ex != null){
System.out.println("SQLState:"+ex.getSQLState());
System.out.println("Message:"+ex.getMessage());
System.out.println("Vendor:"+ex.getErrorCode());
ex=ex.getNextException();
System.out.println("");
}
}
return rs;
}
///modify record
public int recordModify(String Ustr,int Ucount,String[] Uarray){
int i=0;
PreparedStatement pstm;
System.out.println(Ustr+" "+Ucount);
try{
//pstm=con.prepareStatement("update comment set url=?,comment=? where comment.no=8");
pstm=con.prepareStatement(Ustr);
for (int j=1;i<=(Ucount-1);i++){
pstm.setString(j++,Uarray[i]);
System.out.println("Uarray"+(i-1)+Uarray[i]);
//pstm.setString(2,com);
}
i=pstm.executeUpdate();
//
pstm.close();
//rs.close();
}
catch(SQLException ex){
System.out.println("\n***SQLException caught ***\n");
while (ex != null){
System.out.println("SQLState:"+ex.getSQLState());
System.out.println("Message:"+ex.getMessage());
System.out.println("Vendor:"+ex.getErrorCode());
ex=ex.getNextException();
System.out.println("");
}
}
return i;
}
///add a record
public int recordInsert(String add){
int i=0;
PreparedStatement pstm;
try{
pstm=con.prepareStatement(add);
//pstm.setString(1,url);
//pstm.setString(2,com);
i=pstm.executeUpdate();
pstm.close();
//rs.close();
}
catch(SQLException ex){
System.out.println("\n***SQLException caught ***\n");
while (ex != null){
System.out.println("SQLState:"+ex.getSQLState());
System.out.println("Message:"+ex.getMessage());
System.out.println("Vendor:"+ex.getErrorCode());
ex=ex.getNextException();
System.out.println("");
}
}
return i;
}
///delete records
public int recordDelete(String Del){
int j=0;
PreparedStatement pstm;
try{
pstm=con.prepareStatement(Del);
j=pstm.executeUpdate();
pstm.close();
//rs.close();
}
catch(SQLException ex){
System.out.println("\n***SQLException caught ***\n");
while (ex != null){
System.out.println("SQLState:"+ex.getSQLState());
System.out.println("Message:"+ex.getMessage());
System.out.println("Vendor:"+ex.getErrorCode());
ex=ex.getNextException();
System.out.println("");
}
}
return j;
}
///display records
public String[][] recordPrint(ResultSet rs) {
int i;
String tmpstr;
int recordNumber;
String[][] recordArray=null;
try{
ResultSetMetaData rsmd=rs.getMetaData();
int numCols=rsmd.getColumnCount();
int no=0;///no
recordNumber=300;
recordArray=new String[recordNumber][numCols];
//display each column title
for(i=1;i<=numCols;i++){
recordArray[no][i-1]=rsmd.getColumnLabel(i);
}
//display each column data
while (rs.next()){
no++;
for(i=1;i<=numCols;i++){
tmpstr=rs.getString(i);
if(rs.wasNull()){
recordArray[no][i-1]="NULL";
}
else {
recordArray[no][i-1]=tmpstr;
}
}
}
}
catch(SQLException ex){
System.out.println("\n***SQLException333 caught ***\n");
while (ex != null){
System.out.println("SQLState:"+ex.getSQLState());
System.out.println("Message:"+ex.getMessage());
System.out.println("Vendor:"+ex.getErrorCode());
ex=ex.getNextException();
System.out.println("");
}
}
return recordArray;
}
public void display(String[][] recordArray){
int l=recordArray.length;
int ll=recordArray[1].length;
System.out.println("array lenth is :"+l);
System.out.println("record lenth is :"+ll);
for (int i=0;i<l;i++){
for (int j=0;j<ll;j++){
if (recordArray[i][j]!=null)
System.out.print(recordArray[i][j]+",");
//else if break la;
else return;
}
System.out.println(""); ///换行显示
}
}
public void closeConnect(){
try{
con.close();
}
catch(SQLException ex){
System.out.println("\n***SQLException caught ***\n");
System.out.println("SQLState:"+ex.getSQLState());
System.out.println("Message:"+ex.getMessage());
System.out.println("Vendor:"+ex.getErrorCode());
}
}
public void closeResult(){
try{
rs.close();
if (oldStmt!=null)
oldStmt.close();
if (stmt!=null)
stmt.close();
}
catch(SQLException ex){
System.out.println("\n***SQLException caught ***\n");
System.out.println("SQLState:"+ex.getSQLState());
System.out.println("Message:"+ex.getMessage());
System.out.println("Vendor:"+ex.getErrorCode());
}
}
public static void main(String args[])
{
String Ustr="update comment set url=?,comment=? where comment.no=8";
int Ucount=2;
String[] Uarray=new String[2];
Uarray[0]="eeeeeeeeee111";
Uarray[1]="ffffffff222";
int kk=0;
ResultSet rrs=null;
Date ndate;
long ntime;
String sdate;
ntime=System.currentTimeMillis();
System.out.println("the system time is :"+ntime);
ndate=new Date(ntime);
sdate=ndate.toString();
System.out.println("the system date is :"+sdate);
/*
tableSql t=new tableSql();
t.tableInit("Ecmdb");
t.recordInsert("insert into log (type,describe,logtime) values ('aaa','bbb','ccc')");
*/
//rrs=t.recordSql("select * from log");
//t.recordPrint(rrs);
//System.out.println("currentTime is:"+System.currentTimeMillis());
//t.display(t.recordPrint(rrs));
//t.closeResult();
/*try{
rrs.close();
}
catch(SQLException ex){
System.out.println("\n***SQLException caught ***\n");
while (ex != null){
System.out.println("SQLState:"+ex.getSQLState());
System.out.println("Message:"+ex.getMessage());
System.out.println("Vendor:"+ex.getErrorCode());
ex=ex.getNextException();
System.out.println("");
}
}*/
System.out.println("this time is:"+kk++);
//}
//t.recordModify(Ustr,Ucount,Uarray);
//t.recordDelete("delete from comment where comment.no="+args[0]);
//t.recordSql("select * from comment");
//t.closeConnect();
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?