In Netbeans 11.1, I am trying to connect my Java Maven code to a MySql Database but every time I try setting up a connection based on internet tutorials I am getting errors. I have mysql-connector-java-8.0.17.jar in the Dependencies folder. I am very new to java and my Senior Project teammates are having issues figuring out the connection as well.
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class NewJFrame extends javax.swing.JFrame {
//connect to DB
private final String ConnectionString = "jdbc:mysql://54.235.42.66:3306/CIS470db";
private final String UserName = "team6";
private final String Password = "CIS470team6";
//next line has error
private final DBConnect dbConnect;
//next line has error
dbConnect = new DBConnect(ConnectionString, UserName, Password);
first error: cannot find symbol symbol: class DBConnect
Second error: expected
cannot find symbol symbol: class DBConnect
You have not imported DBConnect
in your class which is why the compiler does not know where to find DBConnect
or what it even is.
You also need to do this otherwise it will result in a SQLException
:
try {
dbConnect = new DBConnect(ConnectionString, UserName, Password);
} catch (SQLException e) {
e.printStackTrace();
}
Also, try to implement Singleton Pattern when connecting with Database. It makes it really convenient to follow.
Here's how I did it in one of my older projects:
DatabaseConnection.java
package com.stackoverflow.utility;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import com.stackoverflow.constant.Credentials;
public class DatabaseConnection implements Credentials {
private static DatabaseConnection database;
Connection connection;
public static DatabaseConnection getDatabase() {
if (database == null) {
database = new DatabaseConnection();
}
return database;
}
public Connection getConnection() throws ClassNotFoundException, SQLException {
if (connection == null || connection.isClosed()) {
Class.forName(JDBC_DRIVER);
connection = DriverManager.getConnection(DB_URL, USER, PWD);
}
return connection;
}
}
CloseConnection.java
package com.stackoverflow.utility;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class CloseConnection {
public static void close(ResultSet resultSet) {
if (resultSet != null) {
try {
resultSet.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
public static void close(PreparedStatement preparedStatement) {
if (preparedStatement != null) {
try {
preparedStatement.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
public static void close(Connection connection) {
if (connection != null) {
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
public static void close(PreparedStatement preparedStatement, ResultSet resultSet) {
close(resultSet);
close(preparedStatement);
}
}
Credentials.java
package com.stackoverflow.constant;
public interface Credentials {
static final String JDBC_DRIVER = "com.mysql.cj.jdbc.Driver"; // Database driver
static final String DB_URL = "jdbc:mysql://116.72.10.78:3352/MohitDB"; // Database url
// Database credentials
static final String USER = "user";
static final String PWD = "password";
}
Queries.java
package com.stackoverflow.constant;
public interface Queries {
public static final String AUTHENTICATION_QUERY = "SELECT * FROM TableUser WHERE username = ? AND password = ?";
}
Authentication method
public boolean getAuthentication(Login login) {
try {
PreparedStatement preparedStatement = DatabaseConnection.getDatabase().getConnection()
.prepareStatement(AUTHENTICATION_QUERY);
preparedStatement.setString(1, login.getUsername());
preparedStatement.setString(2, login.getPassword());
ResultSet resultSet = preparedStatement.executeQuery();
if (resultSet.next()) {
close(preparedStatement, resultSet);
return true;
}
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
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.