Posted on 2008-05-09 15:38
橡皮人 阅读(242)
评论(0) 编辑 收藏
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import javax.swing.JOptionPane;
public class DBModel {
static final String driver = "com.mysql.jdbc.Driver";
final static String url = "jdbc:mysql://localhost:3306/databasename";
private String username = "root";
private String password = "textpassword";
private static Connection conn = null;
private String user=null;
private String pwd=null;
public void dbModel() {
try {
Class.forName(driver);
conn = DriverManager.getConnection(url, username, password);
JOptionPane.showMessageDialog(null,"连接成功!");
} catch (ClassNotFoundException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
} catch (SQLException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
} finally {
if (conn == null) {
try {
getConn().close();
} catch (SQLException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}
conn = null;
}
}
}
public Connection getConn() {
return conn;
}
public String getPassword() {
return password;
}
public String getUser() {
return username;
}
public String getPwd() {
return pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
public void setUser(String user) {
this.user = user;
}
}