关闭

JAVA + MYSQL + JDBC Bean范例

标签: jdbcbeanmysqljavastringexception
1962人阅读 评论(2) 收藏 举报
分类:

//连接数据库的Java Bean文件名dbconn.java
package NinGoo;
import java.sql.*;
public class dbconn {
public dbconn() {
}
//declare variable
private Connection conn = null;
ResultSet rs = null;
private String server = "127.0.0.1";
private String port = "3306";
private String db = "test";
private String user = "root";
private String pass = "password";
private String drivername="com.mysql.jdbc.Driver";
private String URL="jdbc:mysql://"+server+":"+port+"/"+db+"?user="+user+"&password="+pass;

public Connection getConn(){//get database connection
try{
Class.forName(drivername).newInstance();
conn = DriverManager.getConnection(URL);
}
catch(Exception e){
e.printStackTrace();
}
return this.conn ;
}

public ResultSet executeSQL(String str) {
try{
Statement stmt = conn.createStatement();
rs = stmt.executeQuery(str);
}
catch(Exception e){
e.printStackTrace();
}
return this.rs;
}
}

 

原文出处:http://ningoo.itpub.net/

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:362741次
    • 积分:4888
    • 等级:
    • 排名:第6048名
    • 原创:124篇
    • 转载:46篇
    • 译文:0篇
    • 评论:162条
    最新评论