📄 高效jdbc.java
字号:
/**
* <p>Title:DataBase Connection </p>
* <p>Description: Provide the function for using the DataBase</p>
* <p>Copyright:Runfor Studio-coolseraph Copyright (c) 2004</p>
* <p>Company: HIT</p>
* @author jinyiqing(Dean)
* @version 1.0
*/
package runfor.db;
import java.io.PrintStream;
import java.sql.*;
//import com.microsoft.jdbc.sqlserver.SQLServerDriver;
public class ConnectionManager
{
private Statement statement;
private String DBDriver;
private String dbURL;
private Connection conn;
ResultSet rs;
private String login;
private String password;
public ConnectionManager()
{
//for access
DBDriver = "sun.jdbc.odbc.JdbcOdbcDriver";
dbURL = "jdbc:odbc:sqlserver";
//The first type connection for sql server
//No DBDriver
//DBDriver =
//dbURL="jdbc:odbc:test";
//The fourth type conneciton for sql server
//DBDriver = "com.microsoft.jdbc.sqlserver.SQLServerDriver";
//jspshop is name of your DataBase
//dbURL = "jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=jspshop";
conn = null;
rs = null;
login="sa";
password="021251723";
}
//the g/s method
public void setDriver (String sDriver)
{
if (sDriver !=null)
DBDriver = sDriver;
}
public String getDriver()
{
return DBDriver;
}
public void setDbURL (String sDbURL)
{
if (sDbURL != null)
dbURL = sDbURL;
}
public String getDbURL()
{
return dbURL;
}
public void setLogin (String sLogin)
{
if (sLogin != null)
login = sLogin;
}
public String getLogin()
{
return login;
}
public void setPassword (String sPassword)
{
if (sPassword != null)
password = sPassword;
}
private String getPassword()
{
return password;
}
// get a connection to the DataBase
private void getConn()
{
if (conn == null)
{
try {
Class.forName(DBDriver).newInstance();
//l.com.microsoft.jdbc.sqlserver.SQLServerDriver.newInstance();
conn = DriverManager.getConnection(dbURL,login,password);
statement = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);//ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE
}
catch (ClassNotFoundException e) {
System.out.println("ConnectionManager: driver unavailable");
conn = null;
}
catch (SQLException e) {
System.out.println("ConnectionManager: driver not loaded "+e.getMessage());
conn = null;
}
catch(Exception e)
{
System.out.print("Exception");
}
if(statement==null&&conn==null)
System.out.print("Statement=null and conn = null\n");
System.out.print("conn="+conn+ "\n");
}
}
//return the connection to the user
public Connection getConnection()
{
getConn();
return conn;
}
public void executeInsert(String s)
{
getConn();
try
{
int i = statement.executeUpdate(s);
}
catch(SQLException sqlexception)
{
System.err.println("ConnectionManager.executeUpdate:" + sqlexception.getMessage());
}
}
public ResultSet executeQuery(String s)
{
getConn();
try
{
//Class.forName(DBDriver).newInstance();
//conn = DriverManager.getConnection(dbURL,login,password);
//statement = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
rs = statement.executeQuery(s);
}
/*catch (ClassNotFoundException e) {
System.out.println("ConnectionManager: driver unavailable");
conn = null;
}*/
catch(SQLException sqlexception)
{
System.err.println("ConnectionManager.executeQuery: " + sqlexception.getMessage());
}
catch(Exception e)
{
System.out.print("Exception");
}
return rs;
}
public void executeDelete(String s)
{
getConn();
try
{
statement.executeUpdate(s);
}
catch(SQLException sqlexception)
{
System.err.println("ConnectionManager.executeDelete: " + sqlexception.getMessage());
}
}
public int executeUpdate(String s)
{
int i = 0;
getConn();
try
{
i = statement.executeUpdate(s);
}
catch(SQLException sqlexception)
{
System.err.println("ConnectionManager.executeUpdate: " + sqlexception.getMessage());
}
return i;
}
public void close()
{
try
{
if(rs != null)
rs.close();
if(conn != null)
conn.close();
}
catch(SQLException sqlexception)
{
System.err.println("ConnectionManager.executeClose: " + sqlexception.getMessage());
}
}
public static void main(String s[])
{
ResultSet rs= new ConnectionManager().executeQuery("select top 10 * from hw where tuijian=1 order by hw_id DESC");
if(rs!=null)
{ System.out.print("Query succed\n");
try{
while(rs.next())
{
System.out.print(rs.getString("hw_name"));
System.out.print(rs.getString("hw_pic")+"\n");
}
}
catch(SQLException e)
{System.out.print("SQLException\n"+e.getMessage()+"\n");e.printStackTrace();}
}
/*new ConnectionManager().executeInsert("insert into book_shelf (bs_kind) values ('人物')");
System.out.println("row inserted");*/
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -