0

我无法从该程序中获得任何输出。你知道为什么吗?我正在使用 NetBeans 8.1 和 wamp 服务器。我设计名为在线作业的数据库。您能否建议我是否使用真正的驱动程序?

图像

package managmentSystem;
import javax.swing.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.sql.SQLException;
/**
 *
 * @author DiyaBangesh
 */
public class ManagmentSystem {

//PreparedStatement pst = null;

//Statement st= null;
   /**
     * @param args the command line arguments
     */
public static void main(String args[]) {
try{

        ResultSet rs;
    Connection conn;
       Class.forName("com.mysql.jdbc.Driver");
       conn = DriverManager.getConnection("jdbc:mysql://localhost/assignment","root","");
       JOptionPane.showMessageDialog(null,"Connection to database is established");
       //return conn;
       Statement st = conn.createStatement();
      String sql ="SELECT * FROM userdetails";
     rs = st.executeQuery(sql);
     while(rs.next()){
     String nam = rs.getString("name");
     String uid = rs.getString("user_id");
     String pwd = rs.getString("password");
     String eid = rs.getString("email_id");
     String con = rs.getString("contact");
     String ut = rs.getString("usertype");

     System.out.println(nam + "   " + uid + "   " + pwd + "  "+ eid + "   "  + con + "   " + ut );

     }   

     conn.close();
        }

   catch (Exception sqlEx){
            System.out.println(sqlEx);
   }

}
}
4

1 回答 1

0

mysql-connector-java-bin.jar 是需要的并且您已经使用过它。确保您使用了正确的数据库名称和正确的密码。将“yourpassword”替换为您的数据库密码。并确保服务器正在运行。

如下所示。

public class ManagmentSystem {

    Connection con;
    Statement s;
    PreparedStatement ps;
    ResultSet rs;


    ManagmentSystem()
    {
        try
        {   Class.forName("com.mysql.jdbc.Driver");            
           con=DriverManager.getConnection("jdbc:mysql://localhost/assignment?user=root&password=yourpassword");
        }
        catch(SQLException s)
        {
            System.out.println("Error in DB Connection");
            s.printStackTrace();
        }
        catch(ClassNotFoundException c)
        {
            System.out.println("Driver not Found");
        }

    }

}
于 2016-06-26T17:24:09.377 回答