【问题标题】:Connecting database (user table) with Java用Java连接数据库(用户表)
【发布时间】:2015-02-09 14:51:59
【问题描述】:

我使用java.swing 获得了这个登录表单,现在我想将我的登录名连接到一个表,即用户详细信息(用户名和密码)都应该存储在本地数据库中以进行登录过程。我尝试创建一个表可以访问然后连接它,但我缺少“系统 DNS”驱动程序。

我没有任何用于数据库连接的实际代码,因为我觉得最好询问最简单的方法,因为我真的很困惑并且尝试时不起作用。

最后,我的问题是:是否可以在 Eclipse 中创建数据库(如在 Visual Studio 中)?什么是正确和简单的方法?创建一个不同的类而不是把它塞进登录页面代码会更好吗?如果是,您如何实际连接两个页面?

public class Loginpage 
{

private JFrame frmGooglePlusExtractor;
private JTextField usernametxt;
private JPasswordField passwordField;

/**
 * Launches the application.
 */
public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                Loginpage window = new Loginpage();
                window.frmGooglePlusExtractor.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

/**
 * Create the application.
 */
public Loginpage() {
    initialize();
}

/**
 * Initialise the contents of the frame.
 */
private void initialize() {
    frmGooglePlusExtractor = new JFrame();
    frmGooglePlusExtractor.setResizable(false);
    frmGooglePlusExtractor.setTitle("Google Plus Extractor");
    frmGooglePlusExtractor.setBounds(100, 100, 326, 200);
    frmGooglePlusExtractor.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frmGooglePlusExtractor.getContentPane().setLayout(new GridLayout(1, 0, 0, 0));

    JPanel panel = new JPanel();
    panel.setBackground(Color.LIGHT_GRAY);
    frmGooglePlusExtractor.getContentPane().add(panel);

    usernametxt = new JTextField();
    usernametxt.setColumns(10);

    JLabel lblUsername = new JLabel("Username");
    lblUsername.setForeground(Color.BLACK);

    JLabel lblPassword = new JLabel("Password");
    lblPassword.setForeground(Color.BLACK);
    lblPassword.setBackground(Color.WHITE);

    JButton btnLogIn = new JButton("Log In");
    btnLogIn.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) 
        {
            loginmsg dlg = new loginmsg();

            dlg.setVisible(true);
            frmGooglePlusExtractor.setVisible(false);   

        }
    });
    btnLogIn.setForeground(Color.BLACK);

    final JButton btnCancel = new JButton("Quit");
    btnCancel.addActionListener(new ActionListener()
    {
        public void actionPerformed(ActionEvent e) 
        {

            if (e.getActionCommand().equals(btnCancel));
            {

            JOptionPane.showMessageDialog(null,"Are you sure you want to leave?");

            frmGooglePlusExtractor.dispose();

            }


        }
    });
    btnCancel.setForeground(Color.BLACK);

    passwordField = new JPasswordField();

    btnNewButton.setForeground(Color.BLACK);

        }
}

但是,我之前尝试过以下操作,但无法正常工作。

public static void main(String[] args) {

    Connection cnctn;

    try{

        // load jdbc driver
        String driver = "com.jnetdirect.jsql.JSQLDriver";

        Class.forName(driver);

        //create connection

        String server = "localhost";
        String port = "55555";
        String database  = server + port;
        String url = "jdbc:JSQLConnect://" + database;
        String username = "username";
        String password  = "password";

        try {
            cnctn = DriverManager.getConnection(url, username, password);

            System.out.println("connected");
        } catch (SQLException e) {

            e.printStackTrace();

            System.out.println("could not connect");
        }

    }

    catch (ClassNotFoundException e)
    {

        System.out.println("could not find db");
    }

}

【问题讨论】:

  • 您没有任何用于连接数据库的代码。
  • 那是因为我真的不知道该怎么做。所以我问的是最简单的方法,任何帮助将不胜感激:)
  • 我添加了一些我尝试过但无法正常工作的代码@user489041

标签: java database login


【解决方案1】:

大部分(或全部)url 使用冒号分隔服务器和端口号:server:port。 数据库应该是一个实际的数据库名称(在您的服务器上)。所以大多数时候 url 应该是这样的:

String url="jdbc:JSQLConnect://"+server+":"+port+"/"+databaseName;

并且只是为了记录最后一个catch子句及其异常通知它找不到驱动程序,而不是数据库。

【讨论】:

  • 你知道我在哪里可以得到mysql-connector-java-4.0.0-bin.jar吗?似乎我缺少该文件,这是数据库连接所必需的。
  • 试图粘贴 lmgtfu 链接...只需在 google 中输入“jdbc mysql 连接器 jar 下载”,其他人必须为您考虑吗?
猜你喜欢
  • 2014-04-11
  • 2014-03-06
  • 1970-01-01
  • 2022-01-19
  • 2011-12-31
  • 1970-01-01
  • 2015-01-29
  • 2012-10-03
相关资源
最近更新 更多