连接 数据库
来源:互联网 发布:maya软件配置要求 编辑:程序博客网 时间:2024/06/05 01:26
已登陆界面为例
先建立源包,创建数据库,统一调用
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);
}
}
- 连接数据库
- 连接数据库
- 连接数据库
- 连接数据库
- 连接数据库
- 连接数据库
- 连接数据库
- 连接数据库
- 连接数据库
- 连接数据库
- 连接数据库
- 连接数据库
- 连接数据库
- 连接数据库
- 连接数据库
- 连接数据库
- 连接数据库
- 连接数据库
- Scala深入浅出进阶经典 第86讲:Scala中For表达式的生成器、定义和过滤器
- Threat Intelligence videos
- noip2011 瑞士轮 (归并排序)
- WinHttpOpenRequest第6个参数如何传?LPCWSTR*
- 朴素贝叶斯
- 连接 数据库
- leetcode:Longest Valid Parentheses 使用动态规划O(n)思路
- 线程间通信-示例
- Scala深入浅出进阶经典 第87讲:Scala中使用For表达式做查询
- Eclipse 配置tomcat8
- epoll简介(一)
- string 和strngbuffer间的相互转换
- Scala深入浅出进阶经典 第88讲:Scala中使用For表达式实现map、flatMap、filter
- 排序算法四:交换排序之冒泡排序