📄 dbconnect.java
字号:
/*
* 创建日期 2005-3-6
*
* TODO 要更改此生成的文件的模板,请转至
* 窗口 - 首选项 - Java - 代码样式 - 代码模板
*/
package store;
/**
* @author wwx
*
* TODO 要更改此生成的类型注释的模板,请转至
* 窗口 - 首选项 - Java - 代码样式 - 代码模板
*/
import java.sql.*;
public class DBConnect {
private Connection conn;
private Statement stmt;
private PreparedStatement prepstmt;
public DBConnect()
throws Exception
{
String sDBDriver="org.postgresql.Driver";
String sConnStr="jdbc:postgresql://localhost:5432/MyStore";
conn = null;
stmt = null;
prepstmt = null;
Class.forName(sDBDriver).newInstance();
String user="postgres";
String password="1234";
conn=DriverManager.getConnection(sConnStr,user,password);
}
public void prepareStatement(String s)
throws SQLException
{
prepstmt = conn.prepareStatement(s);
}
public void prepareStatement(String s, int i, int j)
throws SQLException
{
prepstmt = conn.prepareStatement(s, i, j);
}
public void setString(int i, String s)
throws SQLException
{
prepstmt.setString(i, s);
}
public void setInt(int i, int j)
throws SQLException
{
prepstmt.setInt(i, j);
}
public void setBoolean(int i, boolean flag)
throws SQLException
{
prepstmt.setBoolean(i, flag);
}
public void setDate(int i, Date date)
throws SQLException
{
prepstmt.setDate(i, date);
}
public void setLong(int i, long l)
throws SQLException
{
prepstmt.setLong(i, l);
}
public void setFloat(int i, float f)
throws SQLException
{
prepstmt.setFloat(i, f);
}
public void setBytes(int i, byte abyte0[])
throws SQLException
{
prepstmt.setBytes(i, abyte0);
}
public void clearParameters()
throws SQLException
{
prepstmt.clearParameters();
prepstmt = null;
}
public PreparedStatement getPreparedStatement()
{
return prepstmt;
}
public Statement getStatement()
{
return stmt;
}
public ResultSet executeQuery(String s)
throws SQLException
{
if(stmt != null)
return stmt.executeQuery(s);
else
return null;
}
public ResultSet executeQuery()
throws SQLException
{
if(prepstmt != null)
return prepstmt.executeQuery();
else
return null;
}
public void executeUpdate(String s)
throws SQLException
{
if(stmt != null)
stmt.executeUpdate(s);
}
public void executeUpdate()
throws SQLException
{
if(prepstmt != null)
prepstmt.executeUpdate();
}
public void close()
throws Exception
{
if(stmt != null)
{
stmt.close();
stmt = null;
}
if(prepstmt != null)
{
prepstmt.close();
prepstmt = null;
}
if(conn != null)
{
conn.close();
conn = null;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -