?? tablesql.java
字號:
import java.sql.*;
import java.io.*;
import java.net.*;
import java.util.Date;
import java.text.*;
public class tableSql{
private ResultSet rs;
private Connection con;
private Statement stmt;
private PreparedStatement pstm;
private DatabaseMetaData dma;
///connection database
tableSql(String data_source){
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con=DriverManager.getConnection("jdbc:odbc:"+data_source);
checkForWarning(con.getWarnings());
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;
}
/////////
/*
public Object getField(String sql,String fieldName){
try{
stmt=con.createStatement();
rs=stmt.executeQuery(sql);
Object Field=rs.getString(fieldName);
}
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("");
}
}
}*/
////oper query
public ResultSet recordSql(String sql){
try{
stmt=con.createStatement();
rs=stmt.executeQuery(sql);
}
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 rs;
}
///modify record
public int recordModify(String Ustr,int Ucount,String[] Uarray){
int i=0;
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();
}
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;
try{
pstm=con.prepareStatement(add);
//pstm.setString(1,url);
//pstm.setString(2,com);
i=pstm.executeUpdate();
}
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;
try{
pstm=con.prepareStatement(Del);
j=pstm.executeUpdate();
}
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;
}
/////////
public String[][] getRecords(ResultSet rs) {
int i;
String tmpstr;
int recordNumber;
String[][] recordArray=null;
try{
ResultSetMetaData rsmd=rs.getMetaData();
int numCols=rsmd.getColumnCount();
int no=0;///no use for order of record
recordNumber=100;
recordArray=new String[recordNumber][numCols];
//display each column title
for(i=1;i<=numCols;i++){
recordArray[no][i-1]=rsmd.getColumnLabel(i);
}
//System.out.println("");
//display each column data
while (rs.next()){
no++;
for(i=1;i<=numCols;i++){
//if (i>1) System.out.print(",");
tmpstr=rs.getString(i);
if(rs.wasNull()){
//System.out.print("NULL");
recordArray[no][i-1]="NULL";
//System.out.print(recordArray[no][i-1]);
}
else {
recordArray[no][i-1]=tmpstr;
//System.out.print(recordArray[no][i-1]);
}
}
//System.out.println("");
}
}
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 recordArray;
}
///display records
public void recordPrint(ResultSet rs) {
int i;
String tmpstr;
try{
ResultSetMetaData rsmd=rs.getMetaData();
int numCols=rsmd.getColumnCount();
//display each column title
for(i=1;i<=numCols;i++){
if (i>1) System.out.print(",");
System.out.print(rsmd.getColumnLabel(i));
}
System.out.println("");
//display each column data
while (rs.next()){
for(i=1;i<=numCols;i++){
if (i>1) System.out.print(",");
tmpstr=rs.getString(i);
if(rs.wasNull())
System.out.print("NULL");
else
System.out.print(tmpstr);
}
System.out.println("");
}
}
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("");
}
}
}
public void closeConnect(){
try{
//rs.close();
//stmt.close();
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();
stmt.close();
//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 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 year;
int month;
int day;
int hour;
int min;
int sec;
//Calendar c=Calendar();
Date ndate = new Date();
year=ndate.getYear();
month=ndate.getMonth();
day=ndate.getDay();
hour=ndate.getHours();
System.out.println("now time is:"+ndate.toString());
System.out.println("the year,month,day,hour is:"+year+","+month+","+day);
DateFormat df=DateFormat.getDateInstance(DateFormat.MEDIUM);
String myString=df.format(ndate);
System.out.println("new milli second time is:"+System.currentTimeMillis());
System.out.println("new time is:"+myString);
ndate=new Date(System.currentTimeMillis()-24*60*60*1000);
year=ndate.getYear();
month=ndate.getMonth();
day=ndate.getDay();
hour=ndate.getHours();
System.out.println("the year,month,day,hour is:"+year+","+month+","+day);
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -