连接 数据库
2015-09-27 17:08
351 查看
已登陆界面为例
先建立源包,创建数据库,统一调用
public class DBConnection {
private static Connection con;
public static Connection getCon(){
try {
//加载驱动
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
// 获取链接
con=DriverManager.getConnection("jdbc:sqlserver://localhost:1433;DatabaseName = selectivedb", "sa",
"321");
} catch (ClassNotFoundException ex) {
Logger.getLogger(DBConnection.class.getName()).log(Level.SEVERE, null, ex);
} catch (SQLException ex) {
Logger.getLogger(DBConnection.class.getName()).log(Level.SEVERE, null, ex);
}
return con;
}
//关闭数据库
public void con(){
try {
con.close();
} catch (SQLException ex) {
Logger.getLogger(DBConnection.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
private void txtUserActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
private void btnLoginActionPerformed(java.awt.event.ActionEvent evt) {
try {
DBConnection db=new DBConnection();
Connection con = DBConnection.getCon();
//获得单行文本信息
String user=this.txtUser.getText();
String pwd=new String(this.txtPassword.getPassword());
if(con!=null){
if(this.rdoManager.isSelected()){
PreparedStatement ps=con.prepareStatement("select *from t_manager where sysaccount=? and Syspassword=? ");
ps.setString(1, user);
ps.setString(2, pwd);
ResultSet rs=ps.executeQuery();
if(rs.next()){
JOptionPane.showMessageDialog(this,"欢迎进入选课系统");
this.dispose();
//跳转界面 SelectiveMainFrame
SelectiveMainFrame smf=new SelectiveMainFrame();
this.setVisible(false);
smf.setVisible(true);
}
else{
JOptionPane.showMessageDialog(this,"登录失败,请重新登录");
LoginFrame lg=new LoginFrame();
lg.setVisible(true);
}
}
if(this.rdoTeacher.isSelected()){
JOptionPane.showMessageDialog(this,"登录失败,请重新登录");
LoginFrame lg=new LoginFrame();
lg.setVisible(true);
}
if(this.rdoStudent.isSelected()){
JOptionPane.showMessageDialog(this,"登录失败,请重新登录");
LoginFrame lg=new LoginFrame();
lg.setVisible(true);
}
}
} catch (SQLException ex) {
Logger.getLogger(LoginFrame.class.getName()).log(Level.SEVERE, null, ex);
}
}
先建立源包,创建数据库,统一调用
public class DBConnection {
private static Connection con;
public static Connection getCon(){
try {
//加载驱动
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
// 获取链接
con=DriverManager.getConnection("jdbc:sqlserver://localhost:1433;DatabaseName = selectivedb", "sa",
"321");
} catch (ClassNotFoundException ex) {
Logger.getLogger(DBConnection.class.getName()).log(Level.SEVERE, null, ex);
} catch (SQLException ex) {
Logger.getLogger(DBConnection.class.getName()).log(Level.SEVERE, null, ex);
}
return con;
}
//关闭数据库
public void con(){
try {
con.close();
} catch (SQLException ex) {
Logger.getLogger(DBConnection.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
private void txtUserActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
private void btnLoginActionPerformed(java.awt.event.ActionEvent evt) {
try {
DBConnection db=new DBConnection();
Connection con = DBConnection.getCon();
//获得单行文本信息
String user=this.txtUser.getText();
String pwd=new String(this.txtPassword.getPassword());
if(con!=null){
if(this.rdoManager.isSelected()){
PreparedStatement ps=con.prepareStatement("select *from t_manager where sysaccount=? and Syspassword=? ");
ps.setString(1, user);
ps.setString(2, pwd);
ResultSet rs=ps.executeQuery();
if(rs.next()){
JOptionPane.showMessageDialog(this,"欢迎进入选课系统");
this.dispose();
//跳转界面 SelectiveMainFrame
SelectiveMainFrame smf=new SelectiveMainFrame();
this.setVisible(false);
smf.setVisible(true);
}
else{
JOptionPane.showMessageDialog(this,"登录失败,请重新登录");
LoginFrame lg=new LoginFrame();
lg.setVisible(true);
}
}
if(this.rdoTeacher.isSelected()){
JOptionPane.showMessageDialog(this,"登录失败,请重新登录");
LoginFrame lg=new LoginFrame();
lg.setVisible(true);
}
if(this.rdoStudent.isSelected()){
JOptionPane.showMessageDialog(this,"登录失败,请重新登录");
LoginFrame lg=new LoginFrame();
lg.setVisible(true);
}
}
} catch (SQLException ex) {
Logger.getLogger(LoginFrame.class.getName()).log(Level.SEVERE, null, ex);
}
}
相关文章推荐
- 数据库连接、操作(封装使用)
- 当主库发生宕机,从库如何接管主库
- 《数据管理技术和理论》随堂笔记
- Cygwin编译redis
- SQL的四种连接查询
- 常用的SQL命令
- SQL_SERVER数据库操作
- MySQL 基本操作
- 深入InnoDB的MVCC机制
- SQL Server数据库中批量导入数据的2种方法
- redis有string,hash,list,sets.zsets几种数据类型
- postgresql 数组类型初步实践
- oracle之字符链接 concat(String,String)
- 将ACCESS数据库数据转到SQL server数据库中(通用)
- oracle 之distinct用法
- oracle中的空值问题2 -- null!=null
- mySql and与or的使用
- MySQL——存储过程
- MySQL下载安装、配置与使用(win7x64)
- JDBC数据库连接的简单应用