JBDC and Phpmyadmin / MySQL not connecting - java

Could someone explain to me where I'm going wrong with the following code:
package newdbtet;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
public class NewDBTet {
public static void main(String[] args) throws InstantiationException, SQLException, IllegalAccessException {
try {
System.out.println("MySQL Connect Example.");
Connection conn = null;
String url = "jdbc:mysql://localhost:3306/";
String dbName = "evidence_db";
String driver = "com.mysql.jdbc.Driver";
String userName = "root";
String password = "";
Class.forName(driver).newInstance();
conn = DriverManager.getConnection(url + dbName, userName, password);
System.out.println("Connected to the database");
conn.close();
} catch (ClassNotFoundException ex) {
Logger.getLogger(NewDBTet.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
Exception error:
Jul 16, 2012 2:59:24 PM newdbtet.NewDBTet main
SEVERE: null
java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
Does this mean that I haven't installed the driver / library correctly? Sorry - not the best with Java.

download the MySQL Driver for Eclipse/Java then you should get a .jar driver. then right click on your class and go to build path. finally add the external library to your project, that should solve your problem.

Add the JDBC driver .jar into the class path with a -cp command, i.e. - java -cp MysqlDriver.jar; MyProgram or add the .jar into the build path in your IDE.

Related

After updating the connection details to MariaDB, system throws ClassNotFoundException

I was advised by my web hosting company to change the 'mysql jdbc connection' details to 'mariadb' as per below in the getDBConnection().
I have downloaded the MariaDB jar [mariadb-java-client-1.7.4.jar] compatible for JDK 1.7 & added jar to the project.
Removed the existing MySQL jar [mysql-connector-java-5.1.40-bin.jar] to avoid any conflict. Clean and build the solution and restarted the tomcat server. While submitting the data system still throws below error. Also while debugging at this point, I am receiving null here connection = SoccerUtils.getDBConnection(); preparedStatement1 = connection.prepareStatement("insert into mydatabase.registeruser values(default,?,?,?,?,?,?,?,?,?,?,?)",Statement.RETURN_GENERATED_KEYS);
Any idea Why this is still throwing ClassNotFoundException: com.mysql.jdbc.Driver ?
java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
Oct 15, 2018 10:53:57 PM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet [RegisterServlet] in context with path [/MyFirstJavaTest] threw exception
java.lang.NullPointerException
at com.myfirstjavatest.pkg.RegisterServlet.addPlayer(RegisterServlet.java:134)
at com.myfirstjavatest.pkg.RegisterServlet.doPost(RegisterServlet.java:110)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:647)
//Code below:
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public static Connection getDBConnection() {
String url = "jdbc:mariadb://localhost:3307/";
String dbName = "mydatabase";
String driver = "org.mariadb.jdbc.Driver";
String userName = "root";
String password = "mypassword";
Connection conn = null;
try {
Class.forName(driver).newInstance();
conn = DriverManager
.getConnection(url + dbName, userName, password);
} catch (Exception e) {
System.out.println(e);
} finally {
}
return conn;
}

Connecting to SQL Server 2014 from Android Studio

I have a problem connecting to SQL-server database through from my android project. I have added sqljdbc41.jar file to my /app/libs directory and I have added it to dependencies in my android studio project.
I use following code:
package com.konrad.rezerwacje1;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
public class Database_Console {
public static void openConnection(){
try {
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver"‌​);
String url = "jbdc:sqlserver://127.0.0.1:1433;databaseName=my_db";
Connection con = DriverManager.getConnection(url);
} catch (SQLException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
public static void main(String[] args){
openConnection();
}
}
yet i still get this error
java.sql.SQLException: No suitable driver found for jbdc:sqlserver://127.0.0.1:1433;databaseName=my_db
at java.sql.DriverManager.getConnection(DriverManager.java:689)
at java.sql.DriverManager.getConnection(DriverManager.java:270)
Instead of this :
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver"‌​);
String url = "jbdc:sqlserver://127.0.0.1:1433;databaseName=my_db";
You have to use this :
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
String url = "jdbc:sqlserver://127.0.0.1:1433;DatabaseName=my_db";
Connection con = DriverManager.getConnection(url, "username", "password");
Note the different classname, and the fact that prefix jbdc in the URL has been changed to jdbc.
If it is not a requirement to go with sqljdbc41.jar, then you might consider using the jtds driver for your requirement to connect to SQL Server 2014 with Android Studio. There are tons of articles that can help you start with this set of technologies.
For a primer, here are the details:
Download the JTDS driver from here
Then import this jar into your Android Studio, eg: jtds-1.2.5.jar
Use the following details in your code:
Class.forName("net.sourceforge.jtds.jdbc.Driver").newInstance();
DriverManager.getConnection("jdbc:jtds:sqlserver://127.0.0.1:1433/DATABASE;user=sa;password=p#ssw0rd");

Null URL in JDBC Connection

I am trying to connect with mysql with Java.
I am using db.properties files to get the connection details.
I am kind of new guy to work with the db.properties file. What is the wrong with my code??
It is as below
#mysql DB properties
#DB_DRIVER_CLASS=com.mysql.jdbc.Driver
#DB_URL=jdbc:mysql://localhost:8080/ci_intro
#DB_USERNAME=root
#DB_PASSWORD=
My Java Class file is
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.DriverManager;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Properties;
public class JDBCExample {
public static void main(String[] argv) throws IOException,
ClassNotFoundException, SQLException {
System.out
.println("-------- MySQL JDBC Connection Testing ------------");
Properties props = new Properties();
FileInputStream in = new FileInputStream("db.properties");
props.load(in);
in.close();
String driver = props.getProperty("DB_DRIVER_CLASS");
if (driver != null) {
Class.forName(driver);
}
String url = props.getProperty("DB_URL");
String username = props.getProperty("DB_USERNAME");
String password = props.getProperty("DB_PASSWORD");
Connection con = DriverManager.getConnection(url, username, password);
if (con != null) {
System.out.println("You made it, take control your database now!");
} else {
System.out.println("Failed to make connection!");
}
}
}
I'm getting below error
-------- MySQL JDBC Connection Testing ------------
Exception in thread "main" java.sql.SQLException: The url cannot be null
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at com.avn.notificationengine.JDBCExample.main(JDBCExample.java:33)
Remove "#" in your *.properties file. All that follows "#" is comment. Your *.properties should be:
#mysql DB properties
DB_DRIVER_CLASS=com.mysql.jdbc.Driver
DB_URL=jdbc:mysql://localhost:8080/ci_intro
DB_USERNAME=root
DB_PASSWORD=

Error on testing connection with Mysql Database

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
//This class is for testing connection with mysql database
class JDBCTest {
// path to database is stored in string url
private static final String url = "jdbc:mysql://localhost";
// username is stored in string root
private static final String user = "root"; //username
// password is stored in string password
private static final String password = "swapnil";//password
public static void main(String args[]) {
try {
//i have stored driver in c:\javap\
Class.forName("com.mysql.jdbc.Driver").newInstance();
Connection con = DriverManager.getConnection(url, user, password);
System.out.println("Success");
} catch (Exception e) {
System.out.println("hi");
e.printStackTrace();
}
}
}
whenever I try to run this program I get the exception
java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
I am using mysql database my operating system is windows 7 64 bit. I have included the mysql-connector-java-5.1.22-bin in jdk/jre/lib/ext I have also set up the CLASSPATH Environment variable but nothing work me out
First of all, you should not put anything under the JDK's jre/lib/ext directory.
Instead, use the -cp option when launching your application, and make sure to have the jar of the driver (and not the bin directory) in the classpath:
java -cp mysql-xx.jar;... com.foo.bar.JDBCTest
The URL is incomplete use:
private static final String url = "jdbc:mysql://localhost:3306/databasename";
also as #JB Nizet mentioned do not put jars in jdk's lib.

how to to connect and use JDBCUtil

hi
i want to use Class JDBCUtil in java language and eclips workspace and I have the below code:
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class JDBCUtil {
static final String CONN_STR = "jdbc:hsqldb:hsql://localhost/";
static {
try {
Class.forName("org.hsqldb.jdbcDriver");
} catch (ClassNotFoundException ex) {
System.err.println("Unable to load HSQL JDBC driver");
}
}
public static Connection getConnection() throws SQLException {
return DriverManager.getConnection(CONN_STR);
}
}
but when I run it, I get the below Exception:
Unable to load HSQL JDBC driver
Exception in thread "main" java.lang.NullPointerException
at domain.EnrollCtrl.getCurrentOfferings(EnrollCtrl.java:78)
at ui.UI.main(UI.java:28)
in addition there is UI and EnrollCtrl and another file in this project but the error is in this path.
how can i fix this error? should I do sth for connectin to jdbc:hsqldb:hsql://localhost/?
thank U
your String CONN_STR = "jdbc:hsqldb:hsql://localhost/"; should be
conn = DriverManager.getConnection(jdbc:hsqldb:hsql://localhost/xdb", "sa", "");
In some circumstances, you may have to use the following line to get the driver.
Class.forName("org.hsqldb.jdbcDriver").newInstance();
The chances are that you are missing the hsqldb.jar file from your classpath or some other environmental issue.
It is difficult to be certain as your question is incredibly specific to your code and environment.

Categories

Resources