用Java连接Java(Connect Java with Mysql)
我试图连接一个Neteans Java项目与Mysql数据库,我无法建立连接,我不知道什么可能出错
我的Java代码:
private void setupLoginEventListener() {
loginBtn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
rightFirstText = userName.getText();
rightText = password.getPassword();
if (rightFirstText.isEmpty() && rightText.length == 0) {
JOptionPane.showMessageDialog(JavaApplication6.this, "All fields are required", "Error", JOptionPane.ERROR_MESSAGE);
} else if (rightText.length == 0) {
JOptionPane.showMessageDialog(JavaApplication6.this, "Password is required", "Error", JOptionPane.ERROR_MESSAGE);
} else {
try {
conn = getDBConnection();
pst = conn.prepareStatement("select * from pdie where username =? and password=?");
pst.setString(1, rightFirstText);
pst.setString(2, new String(rightText));
rs = pst.executeQuery();
while (rs.next()) {
JOptionPane.showMessageDialog(JavaApplication6.this, "Login Successfull");
}
} catch (SQLException ex) {
JOptionPane.showMessageDialog(JavaApplication6.this, "Login Failed");
}
}
}
});
}
public Connection getDBConnection() {
Connection con = null;
String url = "jdbc:mysql://localhost:3536/";
String dbName = "projectdb";
String driver = "com.mysql.jdbc.Driver";
String connectUserName = "root";
String connectPassword = "";
try {
Class.forName(driver);
con = DriverManager.getConnection(url + dbName, connectUserName, connectPassword);
System.out.println("CONNECTION ESTABLISHED.");
} catch (ClassNotFoundException | SQLException e) {
System.out.println("CONNECTION COULD NOT BE ESTABLISHED.");
}
return con;
}
有任何想法吗?
它给了我这行中空指针异常的错误
pst = conn.prepareStatement("select * from pdie where username =? and password=?");
I am trying to connect a Neteans Java project with Mysql database and i cannot establish a connection and i do not know what could possibly go wrong
My Java code:
private void setupLoginEventListener() {
loginBtn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
rightFirstText = userName.getText();
rightText = password.getPassword();
if (rightFirstText.isEmpty() && rightText.length == 0) {
JOptionPane.showMessageDialog(JavaApplication6.this, "All fields are required", "Error", JOptionPane.ERROR_MESSAGE);
} else if (rightText.length == 0) {
JOptionPane.showMessageDialog(JavaApplication6.this, "Password is required", "Error", JOptionPane.ERROR_MESSAGE);
} else {
try {
conn = getDBConnection();
pst = conn.prepareStatement("select * from pdie where username =? and password=?");
pst.setString(1, rightFirstText);
pst.setString(2, new String(rightText));
rs = pst.executeQuery();
while (rs.next()) {
JOptionPane.showMessageDialog(JavaApplication6.this, "Login Successfull");
}
} catch (SQLException ex) {
JOptionPane.showMessageDialog(JavaApplication6.this, "Login Failed");
}
}
}
});
}
public Connection getDBConnection() {
Connection con = null;
String url = "jdbc:mysql://localhost:3536/";
String dbName = "projectdb";
String driver = "com.mysql.jdbc.Driver";
String connectUserName = "root";
String connectPassword = "";
try {
Class.forName(driver);
con = DriverManager.getConnection(url + dbName, connectUserName, connectPassword);
System.out.println("CONNECTION ESTABLISHED.");
} catch (ClassNotFoundException | SQLException e) {
System.out.println("CONNECTION COULD NOT BE ESTABLISHED.");
}
return con;
}
any ideas?
it gives me an error for null pointer exception in this line
pst = conn.prepareStatement("select * from pdie where username =? and password=?");
原文:https://stackoverflow.com/questions/27557504
更新时间:2019-12-11 10:06
最满意答案
输入这些行
st=con.createStatement();
String sql="SELECT * FROM TAB";
rs=st.executeQuery(sql);
并尝试打印一个字段; 快速测试问题是否在你准备好的声明中,但可能是在你的conn对象中。
type these lines
st=con.createStatement();
String sql="SELECT * FROM TAB";
rs=st.executeQuery(sql);
and try to print a a field; to test quickly if the problem is in you prepared statement but probably it's in your conn object.
2014-12-19
相关问答
public static Connection getConnection(){ try { Class.forName("com.mysql.jdbc.Driver"); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } Connection conn=null; try { conn=DriverManager.getConnection("jdbc:m
...
DriverManager是一个相当老的做事方式。 更好的方法是通过查看您的应用服务器容器为您配置的方式获取DataSource : Context context = new InitialContext();
DataSource dataSource = (DataSource) context.lookup("java:comp/env/jdbc/myDB");
或直接从数据库驱动程序实例化和配置一个: MysqlDataSource dataSource = new MysqlData
...
问题是我的db中的“匿名”用户没有密码。 删除后问题解决了 the problem was I had "anonymous" user in my db witch had no password. After removing it the problem solved
输入这些行 st=con.createStatement();
String sql="SELECT * FROM TAB";
rs=st.executeQuery(sql);
并尝试打印一个字段; 快速测试问题是否在你准备好的声明中,但可能是在你的conn对象中。 type these lines st=con.createStatement();
String sql="SELECT * FROM TAB";
...
try {
String driverName = "com.mysql.jdbc.Driver";
Class.forName(driverName);
String serverName = "localhost";
String mydatabase = "hpdata";
String url = "jdbc:mysql :// " + serverName + "/" + mydatabase;
String username = "root";
String pas
...
请检查Android应用是否具有访问Internet上的服务的正确权限。
...
Please check if the Android app has the correct permissions for accessing services on the
...
你需要从这里下载mysql包,并将其放置在库中,我将在几分钟内编辑excact步骤 这是连接到数据库的正确语法: try
{
// create a java mysql database connection
String myDriver = "org.gjt.mm.mysql.Driver";
String myUrl = "jdbc:mysql://localhost/test";
Class.forName(myDriver);
Connection conn = D
...
这可能是因为您使用的是旧版本的MySQL驱动程序。 您应该尝试使用最新版本。 要获得最新版本,您可以查看https://mvnrepository.com/artifact/mysql/mysql-connector-java 截至目前,最新版本为8.0.11。 您可以在此处下载或将其添加到您的pom.xml :
mysql
mysql-connector-java
...
127.0.0.1:3306取决于数据库的设置,确保在库中添加MySQL JDBC驱动程序然后尝试此代码。 try{
Class.forName("com.mysql.jdbc.Driver"); // to set the driver
Connection connect = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306", "", "
...