扫一扫
分享文章到微信
扫一扫
关注官方公众号
至顶头条
package online;
import java.sql.*;
public class DBConn {
private static String rootpath = "web发布路径";
private String sample = "sample";
private Connection con = null;
private Statement stmt = null;
ResultSet rs = null;
/***************************************************************/
private static final String DRIVE = "sun.jdbc.odbc.JdbcOdbcDriver";
//暂时使用jdbc-odbc连接//"com.microsoft.jdbc.sqlserver.SQLServerDriver";
private static final String USERNAME = "sa";
private static final String PASSWORD = "123aaa";
private static final String HOST = "http:127.0.0.1:8080/renshi";
/*************************************************************/
//暂时使用jdbc-odbc数据源
private static final String CONNECTION_STRING = "jdbc:odbc:renshi";
//"jdbc:microsoft:sqlserver://localhost;1433;";
public static String getRootPath() {
return rootpath;
}
public DBConn() { //加载驱动
try {
Class.forName(DRIVE);
}
catch (ClassNotFoundException e) {
System.err.println("DBConn():" + e.toString());
}
catch (Exception e) {
System.err.println("DBConn():" + e.toString());
}
}
public Connection getConnection() { //得到连接
try {
String strUrl = CONNECTION_STRING;
/***********周五晚改动****************************************/
//+ "DatebaseName=renshi," + USERNAME +"," + PASSWORD;
con = DriverManager.getConnection(strUrl, this.USERNAME, this.PASSWORD);
}
catch (Exception e) {
con = null;
}
return con;
}
public void dropConnection() { //关闭连接
try {
closeStmt();
con.close();
}
catch (Exception ignored) {
}
finally {
con = null;
}
}
public ResultSet executeQuery(String sql) { //执行sql查询
ResultSet rs = null;
try {
con = getConnection();
stmt = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
ResultSet.CONCUR_READ_ONLY);
rs = stmt.executeQuery(sql);
}
catch (SQLException ex) {
System.err.println("DBConn.executeQuery():" + ex.getMessage());
}
return rs;
}
public int executeUpdate(String sql) { //执行sql更新语句
int i=0;
stmt = null;
rs = null;
try {
con = getConnection();
stmt = con.createStatement();
i= stmt.executeUpdate(sql);
stmt.close();
con.close();
}
catch (SQLException ex) {
System.err.println("DBConn:executeUpdate(0:" + ex.getMessage());
}
return i;
}
public void execute(String sql) { //执行sql语句
stmt = null;
rs = null;
try {
con = getConnection();
stmt = con.createStatement();
stmt.execute(sql);
stmt.close();
con.close();
}
catch (SQLException ex) {
System.err.println("DBConn:excute():" + ex.getMessage());
}
}
public void closeConn() { //关闭sql连接
try {
stmt.close();
}
catch (SQLException e) {
e.printStackTrace();
}
}
public void closeStmt() { //关闭sql连接
try {
con.close();
}
catch (SQLException e) {
e.printStackTrace();
}
}
/**
* main
*/
public static void main(String[] args) throws SQLException {
DBConn one=new DBConn();
ResultSet rs=one.executeQuery("select * from PS_INFO");
while(rs.next()){
System.out.println(rs.getString(2));
}
}
}
Trackback: http://tb.blog.csdn.net/TrackBack.aspx?PostId=762762
如果您非常迫切的想了解IT领域最新产品与技术信息,那么订阅至顶网技术邮件将是您的最佳途径之一。
现场直击|2021世界人工智能大会
直击5G创新地带,就在2021MWC上海
5G已至 转型当时——服务提供商如何把握转型的绝佳时机
寻找自己的Flag
华为开发者大会2020(Cloud)- 科技行者