jdbc connection mysql

Posted on 2008-05-09 15:38 橡皮人 阅读(243) 评论(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;
}
}

只有注册用户登录后才能发表评论。


网站导航:
 

posts - 28, comments - 5, trackbacks - 0, articles - 0

Copyright © 橡皮人