* To change this template, choose Tools | Templates
* and open the template in the editor.
*/package ******
import java.io.PrintWriter
import java.sql.*/**
* @author wfg
*/
public class DB_Conn {
private String driverName = "com.mysql.jdbc.Driver" //JDBC驱动
private String userName = "root" //数据库用户名
private String userPwd = "*****" //数据库用户密码
private String dbName = "******"//数据库名
private String url = "jdbc:mysql://localhost:3306/"+dbName+"?user="+userName+
"&password="+userPwd //数据库连接字符串
private Connection conn = null//数据库连接对象
public Statement sm = null//数据库语句对象
private PrintWriter out = null //建立数据库连接函数
public void ConnectDB(){
try{
Class.forName(driverName).newInstance()
conn = DriverManager.getConnection(url)
sm = conn.createStatement()
}
catch(Exception e){
e.printStackTrace()
out.print("数据库连接失败!")
}
}//释放数据库连接函数
public void CloseDB(){
try{
if(sm != null){
sm.close()
}
conn.close()
}
catch(SQLException SqlE){
SqlE.printStackTrace()
out.print("数据库关闭失败!")
}
}
}
这是先建立连接
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)