2017-04-26 22 views
0

我想将db连接与其他类分开,因此我不需要再为每个要创建的类编写db连接。我希望登录类能够访问不同类的数据库连接

这是数据库连接类

package bloodbank; 

import java.sql.Connection; 
import java.sql.DriverManager; 
import java.sql.PreparedStatement; 
import java.sql.ResultSet; 
import java.sql.SQLException; 

public class dbconnection { 
    PreparedStatement pst = null; 
    ResultSet rs = null; 

public dbconnection() {} 

public void connect() { 
try { 
    Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); 
    Connection con = 
DriverManager.getConnection("jdbc:sqlserver://localhost:1433; 
databaseName=BloodManagementSystem;user=yusuf;password=ali1234"); 
    } catch (ClassNotFoundException | SQLException e) { 
    } 

} 

public PreparedStatement prepareStatement(String select__from_Users_where_Username_and_Pas) { 
    throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. 
} 
} 

这是登录类登录按钮

import bloodbank.dbconnection; 
import java.awt.HeadlessException; 
import java.sql.PreparedStatement; 
import java.sql.ResultSet; 
import java.sql.SQLException; 
import javax.swing.JOptionPane; 

public class login extends javax.swing.JFrame { 

public login() { 
    initComponents(); 
} 
@SuppressWarnings("unchecked") 

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed 

    try 
    { 

     dbconnection con = new dbconnection(); 
     con.connect(); 

    PreparedStatement pst = con.prepareStatement("Select * from Users where Username=? and Password=?"); 
    pst.setString(1, jTextField1.getText()); 
    pst.setString(2, jTextField2.getText()); 
    ResultSet rs = pst.executeQuery(); 
    if(rs.next()) { 
     JOptionPane.showMessageDialog(null, "Username and Password correct"); 
     Mainform field = new Mainform(); 
     field.setVisible(true); 
     setVisible(false); 


     } else { 
     JOptionPane.showMessageDialog(null, "invalid username and password"); 
     } 
    } 
    catch(SQLException | HeadlessException e){ 
       JOptionPane.showMessageDialog(null, e); 

    } 


    } 

public static void main(String args[]) { 

    java.awt.EventQueue.invokeLater(() -> { 
     new login().setVisible(true); 
    }); 
} 

} 
+0

欢迎来到Stack Overflow!请[参观](http://stackoverflow.com/tour)以查看网站的工作原理和问题,并相应地编辑您的问题。另请参阅:[为什么“有人可以帮我吗?”不是一个真正的问题?](http://meta.stackoverflow.com/q/284236) –

+0

是的,这是一个好主意。问题是什么? – MadProgrammer

+0

我想有人来测试这个代码,因为当我运行它时,它显示了很多像这样的错误 –

回答

0

我假设你希望只使用一个在您的应用程序的连接。您应该为dbconnection使用单身风格的类。有一个private static Connection cnx变量来存储连接,并检查连接是否建立,然后每次尝试创建一个新连接。

public class dbconnection { 
    private static Connection cnx = null; 

    public Connection getConnection() { 
     if(cnx == null) { 
      cnx = (... initialize the connection ...) 
     return cnx 
    } 
} 

呼叫Connection con = dbconnection.getConnection()去就地当前.connect()的数据库连接的参考。

您也不应将数据库凭据存储在源代码中。将秘密账户和密码硬编码到您的软件中非常方便 - 对于熟练的反向工程师。如果所有软件的密码相同,那么当密码不可避免地变为已知时,每个客户都会变得易受攻击。而且因为它是硬编码的,所以修复这是一个巨大的痛苦。

对于一个快速和肮脏的解决方案/小破解它很好,但出于生产目的,您应该将配置信息(包括密码)存储在应用程序在启动时读取的单独文件中。这是防止密码因反编译而泄漏的唯一真正方法(从不将它编译到二进制文件中开始)

+0

我是新程序员,但我感谢你的小技巧。 –

0

您可以使您的连接静态并在首次调用getConnection方法时初始化它。这样的代码:

package com.stackoverflow.json; 

import java.sql.Connection; 
import java.sql.DriverManager; 
import java.sql.PreparedStatement; 
import java.sql.ResultSet; 
import java.sql.SQLException; 

public class dbconnection { 

    PreparedStatement pst = null; 
    ResultSet rs = null; 
    private static Connection con; 

    public dbconnection() { 
    } 

    private static void connect() { 
     try { 
      Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); 
      con = DriverManager.getConnection(
        "jdbc:sqlserver://localhost:1433; databaseName=BloodManagementSystem;user=yusuf;password=ali1234"); 

     } catch (ClassNotFoundException | SQLException e) { 
     } 

    } 

    public PreparedStatement prepareStatement(String select__from_Users_where_Username_and_Pas) { 
     throw new UnsupportedOperationException("Not supported yet."); 
    } 

    public static Connection getConnection() { 
     if (con == null) 
      connect(); 
     return con; 
    } 
} 

你还可以创建类中调用DBUtils它包含了所有的方法来查询数据库的JDBC。