?? conn.java
字號:
package com.yourcompany.struts;
import java.sql.*;
public class Conn {
public String driverName="sun.jdbc.odbc.JdbcOdbcDriver";
public String dbUrl="jdbc:odbc:bookShop";
private String userName="";
private String userPwd="";
public Connection conn;
public Statement st;
private Pageable rs;
//連接數據庫,執行語句,返回得到的記錄項--用于查詢
public void init(String sql) throws Exception
{
Class.forName(driverName);
conn=DriverManager.getConnection(dbUrl, null,null);
st=conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_UPDATABLE);
rs=new PageableResultSet2(st.executeQuery(sql));
}
//判斷記錄欺罔是否為空,若為空則調用函數 init(sql)
public Pageable getRs(String sql) throws Exception
{
if(rs==null)
{
init(sql);
}
return rs;
}
//執行 添加、更新、刪除,無返回
public void exeUpd(String sql) throws Exception
{
Class.forName(driverName);
conn=DriverManager.getConnection(dbUrl,null,null);
st=conn.createStatement();
st.executeUpdate(sql);
}
//關閉 查詢 連接
public void closeRs() throws Exception
{
rs.close();
st.close();
conn.close();
}
//關閉 添加、更新、刪除 連接
public void closeNoRs() throws Exception
{
st.close();
conn.close();
}
public static void main(String[] args)
{
Connection conn = null;
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
System.out.println("正確");
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("連接問題1");
}
try {
conn = DriverManager.getConnection("jdbc:odbc:bookShop", null,null);
System.out.println("正確");
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("連接問題2");
}
try {
Statement st=conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_UPDATABLE);
System.out.println("正確");
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("連接問題3");
}
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -