I'm newbie at Java netbeans , I am trying to load mysql driver, but my Netbeans IDE don't show libary folder in my Project.
try {
Class.forName("com.mysql.jdbc.Driver");
System.out.println("Its My First Project");
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/mychakki", "root", "password");
System.out.println("Connection Established ");
} catch (ClassNotFoundException | SQLException e) {
System.out.println("Exception :" +e.getMessage());
}
Related
I started working today with the JDBC Microsoft SQL Server driver and I am always getting the error that no suitable driver has been found.
Here is my code:
/*
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
*/
String url = "jdbc:sqlserver://" + HOST + ":" + PORT + ";databaseName=" + DATABASE;
try {
connection = DriverManager.getConnection(url, USERNAME, PASSWORD);
} catch (SQLException e) {
System.err.println("[SQL] Error in connection: " + url);
e.printStackTrace();
}
I also used Class.forName(...), but since it will be running on other people's servers I don't want to use this option (This option also did not work). Help would be appreciated, have a nice weekend
As said in the title of the question, JDBC seems to work only in test classes, can't explain why.
public static Connection getConnection()
{
try {
String connectionString = "jdbc:mysql://localhost/" + database + "?" +
"user=" + sqlUser + "&password=" + sqlPassword;
return DriverManager.getConnection(connectionString);
} catch (SQLException ex) {
Logger.getLogger(ConnectionLoader.class.getName()).log(Level.SEVERE, null, ex);
}
return null;
}
The following test passes:
assertNotNull("Connection must not be null.", ConnectionLoader.getConnection());
But when debugging the project fails with SQLException saying no suitable driver found.
Here's a screenshot of my project in Netbeans, as you can see I included the mysql jar in the libraries.
Am I missing something?
You need to load MySQL driver.
try{
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://x.x.x.x:3306/databasename", "username", "password");
...
}catch(Exception ex){
ex.printStackTrace();
}
I am trying to connect a Java program to a database in localhost. My problem seems to be very simple, but I cannot find any answer.
When I try to compile, I get the following error:
DriverManager.getConnection cannot be resolved to a type
On the following code:
try{
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException ex){
System.out.println("Error found " + ex);
ex.printStackTrace();
}
// Here is where the error pops up
Connection connection = new DriverManager.getConnection("jdbc:mysql//localhost:3306/project3", "root", "root");
I am assuming I screwed up some installation, but I am not completely sure what.
Thank you for any help.
why new here?
Just use
DriverManager.getConnection("jdbc:mysql//localhost:3306/project3",
"root", "root");
getConnection() is static method. just call it as above.
Just Edit your code like this
try{
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException ex){
System.out.println("Error found " + ex);
ex.printStackTrace();
}
Connection connection= DriverManager.getConnection("jdbc:mysql://localhost:3306/project3", "root", "root");
Avoid new And put : after jdbc:mysql
I have a simple Web Application setup inside netbeans IDE. Having a spot of trouble when trying to connect the persistence layer.
The database itself is Apache Derby. I've set the DERBY.jar to the classpath, and I've added both DERBY.jar and DERBYCLIENT.jar to the lib folder of the application.
The code is appended below. The database path is set correctly, as are the username and password variables.
private static final String DB_URL = "jdbc:derby://localhost:1527/myDB";
public void dbConnection(){
System.out.println("Attempting to establish a connection to a database");
try{
Class.forName("org.apache.derby.jdbc.EmbeddedDriver").newInstance();
}catch (ClassNotFoundException e) {
System.out.println(e.toString());
} catch (InstantiationException ex) {
Logger.getLogger(DBManager.class.getName()).log(Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
Logger.getLogger(DBManager.class.getName()).log(Level.SEVERE, null, ex);
}
System.out.println("Driver Loaded");
try{
connection = DriverManager.getConnection(DB_URL, USERNAME, PASSWORD);
System.out.println("Successfully Connected to DERBY DATABASE");
} catch (SQLException e) {
System.out.println(e.toString());
System.out.println("Could not connect to the DERBY DATABASE");
}
}
This problem was solved by using the client driver as opposed to the Embedded Driver inside of the persistence setup code.
My IDE is Eclipse Indigo. I get this when I was trying to connect:
java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
And here is my code.
public class TPCH
{
public static void main(String[] args)
{
String userName = "tpch";
String password = "tpch";
Connection conn = null;
Properties connectionProps = new Properties();
connectionProps.put("user", userName);
connectionProps.put("password", password);
try
{
Class.forName("com.mysql.jdbc.Driver");
}
catch (Exception e)
{
e.printStackTrace();
}
try {
conn = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/",
connectionProps);
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
System.out.println("Error connecting to db");
}
}
}
I think JDBC is not imported. I tried to import it by
preference -> java -> build path -> user library -> add jars
But I still got that exception.
That's not how you add JARs to the classpath in Eclipse.
You have to right-click on your project, select Java Build Path > Libraries and add a JAR file. For MySQL, you'd need the MySQL Connector J.