简体   繁体   中英

java database connectivity in netbeans

[enter image description here][1]while installing mysql on pc, i got an error message that "port 3306 is currently in use. provide another port" so i changed the port number to 3305. now i am trying to connect java project in netbeans ide to mysql, but it ain't working even though i tried with both the port numbers.

enter code here

   String pword= password.getText();
   String name= tf.getText();
   String str= null;
   Statement stmt= null;
   ResultSet rs= null;
    Connection conn=null;
   try{
       Class.forName("java.sql.Driver");
       String pwd= "mysql";
       String uid="root";
       String url="jdbc:mysql://localhost:3306/project";
       try{
            conn= (Connection)DriverManager.getConnection(url,uid,pwd);
            stmt= conn.createStatement();
            String sql= "SELECT * FROM login WHERE name='" + name+ "'";
            rs= stmt.executeQuery(sql);
            rs.next();
            str= rs.getString("password");
            if(str.equals(pword))
            {
             menu m= new menu();
             m.setVisible(true);
             this.setVisible(false);

            }
            else
               JOptionPane.showMessageDialog(null, "Incorrect username or password!");
            rs.close();
            stmt.close();
            conn.close();
          }
       catch(Exception e)
       {
           JOptionPane.showMessageDialog(null, "Incorrect username or password!");
       }

   } 
   catch(Exception e)
   {
       JOptionPane.showMessageDialog(null, "Error in Connectivity");
   }

I did something like this (for MS SQL server) recently however I don't think you specified the database name

`String url ="jdbc:sqlserver://YourPCname\\instancename;databaseName=yourDBname";` 

(note you'd need to change "sqlserver" to "mysql")

Also make sure that your server is running.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM