【问题标题】:unable to link derby DB with my java app on netbeans无法将 derby DB 与我在 netbeans 上的 java 应用程序链接
【发布时间】:2018-06-22 10:10:24
【问题描述】:

我已经为一个应用项目创建了一个数据库,并且我意识到该应用的所有窗口,当我尝试将 derby 数据库链接到我的项目时它不起作用!我已经编写了几个代码来链接数据库,但仍然无法正常工作(起初我使用我创建的数据库,然后我尝试将嵌入式数据库链接到我的项目但没有)这里有什么试过了!!我错过了什么吗?

  try {  Class.forName("org.apache.derby.jdbc.EmbeddedDriver");
    Connection cnx = DriverManager.getConnection("jdbc:derby:C:\\Users\\user pc\\AppData\\Roaming\\NetBeans\\Derby\\projetihm","root","root");
    Statement stm= cnx.createStatement();
    String qst = "Select * from ROOT.USERAPP where username='"+uname.getText()+"'";
    ResultSet rst = stm.executeQuery(qst);
    if (rst.next())
         { JOptionPane.showMessageDialog(null, "Connexion successful");
                 }
    else {JOptionPane.showMessageDialog(null, "Wrong password or username"); }
 }
 catch (Exception e ) {};

我也试过这个

Try{  Connection cnx= DriverManager.getConnection("jdbc:derby://localhost:1527/projetihm","root","root");
Statement stm= cnx.newStatement();
ResultSet rst = stm.executeQuery("Select * From userapp ");
while (rst.next())
   if (rs.getString(1).equals(uname.getText()) && rst.getString(2).equals(pass.getText()))
        { JOptionPane.showMessageDialog(null, "Wrong password or username try again");
                 }
    else {JOptionPane.showMessageDialog(null, "Wrong password or username try again"); }
 }
 catch (Exception e ) {};

【问题讨论】:

  • 首先请将catch (Exception e) {}替换成catch (Exception e) { e.printStackTrace();},看看有没有异常。
  • 它说数据库的启动失败,但我不知道是什么问题是它的 url 路径或其他什么!!
  • 能否将堆栈跟踪添加到您的问题中?
  • 没关系,我已经解决了
  • 我将发布我在问题部分中使用的代码

标签: java jdbc derby


【解决方案1】:

我这样解决了,现在可以了!

try {   
    Class.forName("org.apache.derby.jdbc.EmbeddedDriver").newInstance();
    Connection cnx = DriverManager.getConnection("jdbc:derby://localhost:1527/projetihm","root","root");
    Statement stm= cnx.createStatement();
    ResultSet rst = stm.executeQuery("Select * From userapp");
    while (rst.next())
       if (rst.getString(1).equals(uname.getText()) && rst.getString(4).equals(pass.getText()))
    {    this.setVisible(false);
        all a = new all(); a.setVisible(true);
             }
       else {JOptionPane.showMessageDialog(null, "Wrong password or username "); }

    } catch (Exception e) {  e.printStackTrace(); }

【讨论】:

    猜你喜欢
    • 2015-06-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多