conn.java~1~
来自「教学管理系统的JAVA实现代码」· JAVA~1~ 代码 · 共 102 行
JAVA~1~
102 行
package base.db;
import java.sql.*;
public class Conn
{
private static Connection con;
private Statement stmt;
private ResultSet rs;
private static String drivername="com.mysql.jdbc.Driver";
private static final String url="jdbc:mysql://localhost/test?user=root&password=yousufu";
public static synchronized Connection getCon() throws Exception{
try{
Class.forName(drivername);
con=DriverManager.getConnection(url);
return con;
}
catch(SQLException e){
System.err.println(e.getMessage());
throw e;
}
}
public Statement getStmtread() {
try {
con = getCon();
stmt = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_UPDATABLE);
return stmt;
} catch (Exception e) {
System.err.println(e.getMessage());
e.printStackTrace();
}
return null;
}
/**
*output parameter: Data
*input parameter: SQL select sentence
*modify time: 4/29/2006
*/
public ResultSet getRs(String sql) {
try {
stmt = getStmtread();
rs = stmt.executeQuery(sql);
return rs;
} catch (Exception e) {
System.err.println(e.getMessage());
e.printStackTrace();
}
return null;
}
/**
*output parameter: not in order select in str SQL
*modify time: 4/29/2006
*/
public Statement getStmt() {
try {
con = getCon();
stmt = con.createStatement();
return stmt;
} catch (Exception e) {
System.err.println(e.getMessage());
e.printStackTrace();
}
return null;
}
/**
*method explain: Close DataBase Connection
*modify time: 4/29/2006
*/
public synchronized void close() {
try {
if (rs != null) {
rs.close();
rs = null;
}
} catch (Exception e) {
System.err.println(e.getMessage());
e.printStackTrace();
}
try {
if (stmt != null) {
stmt.close();
stmt = null;
}
} catch (Exception e) {
System.err.println(e.getMessage());
e.printStackTrace();
}
try {
if (con != null) {
con.close();
con = null;
}
} catch (Exception e) {
System.err.println(e.getMessage());
e.printStackTrace();
}
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?