?? databasemanager.java
字號(hào):
import java.io.*;
import java.net.*;
import java.util.*;
import java.net.URL;
import java.sql.*;
import javax.swing.table.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class DataBaseManager
{
java.sql.Connection con;
ResultSet rs;
Statement stmt;
//String search_str=subString(s);
/*為數(shù)據(jù)庫(kù)管理器的構(gòu)造函數(shù),進(jìn)行數(shù)據(jù)庫(kù)鏈接*/
public DataBaseManager(String l)
{
try
{
try
{
Class.forName( "sun.jdbc.odbc.JdbcOdbcDriver" );/*加載指定的驅(qū)動(dòng)程序*/
}
catch(java.lang.ClassNotFoundException e)
{
System.err.print("ClassNOtFoundException:");
System.err.println(e.getMessage());
System.err.println("Failed to load JDBC/ODBC driver." );
}
con=DriverManager.getConnection(l);
DatabaseMetaData dma=con.getMetaData();
System.out.println("連接的數(shù)據(jù)庫(kù):"+dma.getURL());
System.out.println("Driver :"+dma.getDriverName());
System.out.println("Version :"+dma.getDriverVersion());
stmt=con.createStatement();
}
catch(SQLException ex)
{
while(ex!=null)
{
System.out.println("數(shù)據(jù)庫(kù)異常被捕獲了");
System.out.println(ex.getSQLState());
System.out.println(ex.getMessage());
System.out.println(ex.getErrorCode());
ex=ex.getNextException();
}
}
}
/*查找函數(shù)*/
public String querySql(String buf,String table,String sbuf,String s)
{
String search_str=s;
String s_buf=sbuf;
String tab=table;
String temp="%"+search_str+"%";
//System.out.println(temp);
//String strSQL="select * from titles where pub_id like '"+temp+"'";
String strSQL="select * from "+tab+" where "+s_buf+" like "+"\'"+temp+"\'";
//System.out.println(strSQL);
try
{
rs=stmt.executeQuery(strSQL);
rs.next();
//System.out.println("This is content "+rs.getString(buf));
String result=rs.getString(buf);
return result;
}
catch(SQLException ex)
{
while(ex!=null)
{
System.out.println("數(shù)據(jù)庫(kù)異常被捕獲了");
System.out.println(ex.getSQLState());
System.out.println(ex.getMessage());
System.out.println(ex.getErrorCode());
ex=ex.getNextException();
}
return null;
}
}
public boolean updateSql(String buf,String s)
{
String search_str=s;
String temp="%"+search_str+"%";
String strSQL="select * from titles where pub_id like '"+temp+"'";
try{
stmt.executeUpdate(strSQL);
con.commit();
return true;
}
catch(SQLException sqle)
{
System.out.println(sqle.toString());
return false;
}
}
// public Vector additems
public boolean insertSql(String table,String buf,String s)
{
String tab=table;
String write_Str=s;
String write_buf=buf;
String strSQL="INSERT INTO "+tab+" ("+write_buf+") "+" values "+" ("+write_Str+")";
System.out.println(strSQL);
try
{
stmt.executeUpdate(strSQL);
return true;
}
catch(SQLException ex)
{
while(ex!=null)
{
System.out.println("數(shù)據(jù)庫(kù)異常被捕獲了");
System.out.println(ex.getSQLState());
System.out.println(ex.getMessage());
System.out.println(ex.getErrorCode());
ex=ex.getNextException();
System.out.println(ex.toString());
}
return false;
}
}
/*刪除記錄函數(shù)*/
public boolean deleteSql(String table,String buf,String s)
{
String tab=table;
String strSQL="DELETE FROM "+tab+" where "+ buf +"=" +"\'"+s+"\'";
//System.out.println(strSQL);
try
{
stmt.executeUpdate(strSQL);
System.out.println("刪除成功");
return true;
}
catch(SQLException ex)
{
while(ex!=null)
{
System.out.println("數(shù)據(jù)庫(kù)異常被捕獲了");
System.out.println(ex.getSQLState());
System.out.println(ex.getMessage());
System.out.println(ex.getErrorCode());
ex=ex.getNextException();
//System.out.println(ex.toString());
}
return false;
}
}
/*斷開(kāi)與數(shù)據(jù)庫(kù)的鏈接*/
public void closeConnection()
{
try
{
con.close();
System.out.println("已退出數(shù)據(jù)庫(kù)");
}
catch(SQLException ex)
{
while(ex!=null)
{
System.out.println("數(shù)據(jù)庫(kù)異常被捕獲了");
System.out.println(ex.getSQLState());
System.out.println(ex.getMessage());
System.out.println(ex.getErrorCode());
ex=ex.getNextException();
}
}
}
public static void main(String args[])
{
String l="jdbc:odbc:driver={Microsoft Access Driver (*.mdb)};DBQ=gw.mdb";
String s="0";
DataBaseManager db=new DataBaseManager(l);
String buf="BOX";
String a=db.querySql(buf,"SMS","ID",s);
System.out.println(a);
}
}
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -