Java MySQL connection not working: No suitable driver found [duplicate] - java

This question already has answers here:
Connect Java to a MySQL database
(14 answers)
Closed 2 years ago.
So I am trying to make a connection with JDBC using XAMPP, but it doesn't work, What am I doing wrong here?
public static void main(String args[]) {
try {
Connection myConn = DriverManager.getConnection(
"http://localhost/phpmyadmin/sql.php?db=a3_eindopdracht_2&table=namen&pos=0", "", "");
Statement myStm = myConn.createStatement();
ResultSet myRs = myStm.executeQuery("SELECT * FROM namen");
while (myRs.next()) {
System.out.println(myRs.getString("voornaam") + " " + myRs.getString("achternaam"));
}
} catch (Exception exc) {
exc.printStackTrace();
}

Firstly, you don't find Driver. You have to load them, by calling the Drive class like that :
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
// Cannot find driver for MySQL
}
Then, you are trying to connect to your databse with HTTP protocol. But, DB have they own (with port 3306 by default), so you have to use address like that:
jdbc:mysql://myserver.com/schema
Finally: don't forget to add username and password on last 2 fields of getConnection method

Related

failed to connect Java Project with MySQL Workbench (TIMEZONE error) [duplicate]

This question already has answers here:
MySQL JDBC Driver 5.1.33 - Time Zone Issue
(35 answers)
Closed 3 years ago.
i have homework, and i need use mysql and java.
i need acces to my table on mysql workbench, the table employeee,
i download the driver and do every thing right,
but i get error of :
The server time zone value '???? ???? ???????' is unrecognized or represents more than one time zone. You must configure either the server or JDBC driver (via the serverTimezone configuration property) to use a more specifc time zone value if you want to utilize time zone support.
if its relevant this is my code.
import java.sql.*;
public class Driver {
public static void main(String[] args) {
Connection myConn = null;
try {
myConn =(Connection) DriverManager.getConnection("jdbc:mysql://localhost:3306/employee",
"root", "root");
//create statement
Statement myStmt = myConn.createStatement();
if (myConn !=null) {
System.out.println("connection succsesful");
//excute sql query
ResultSet myRs = myStmt.executeQuery("select * from project");
//process the result set
while(myRs.next()) {
System.out.println(myRs.getString("DNUM") + "," + myRs.getString("PNUMBER"));
}
}
}
catch(Exception exc) {
System.out.println("connection failed");
exc.printStackTrace();
}
}
}
Change the first argument to DriverManager.getConnection from "jdbc:mysql://localhost:3306/employee" to "jdbc:mysql://localhost:3306/employee?serverTimezone=UTC.
If you don't want to use UTC, substitute UTC for the timezone you want the database to record/display times in.
So in the end, I downloaded mysql-connector-java-5.1.48 driver and now its work well.
Your next question - how i can check if he input Incorrect query on sql language?
I think if your sql query syntax is wrong, you will receive MySQLSyntaxErrorException.
And you can check ResultSet is empty.
ResultSet rs = stmt.executeQuery(yourQuery);
if(rs.next()==false)
System.out.println("ResultSet is empty");
else{
while(rs.next()){
//get data from table
}
}

Always getting Run Time Error #Java [duplicate]

This question already has answers here:
Removal of JDBC ODBC bridge in java 8
(5 answers)
Manipulating an Access database from Java without ODBC
(1 answer)
Closed 4 years ago.
So I'm getting run time error always when I tried to login. Below is the code. I'm using Java JRE 8 Eclipse to compile it. Can anyone please let me know what's the problem? I've omitted most of the code. Link to full code : https://codeshare.io/5gNyZw . Already tried Removal of JDBC ODBC bridge in java 8 and how to use JDBC in java 8 but nothing works
try
{
String database="StegoKeys.mdb";
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
String url="jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ=" + database + ";PWD=cegospdv";
Connection con=DriverManager.getConnection(url);
String sql="select * from keys";
PreparedStatement ps=con.prepareStatement(sql);
ResultSet rs=ps.executeQuery();
while(rs.next())
{
if(txtKey.getText().equals(rs.getString("key")))
{
id=rs.getString("uname");
if(txtname.getText().equals(id))
{
int stat=Integer.parseInt(rs.getString("status"));
flag=1;
if(stat==1)
{
button1.setEnabled(true);
button2.setEnabled(true);
btnadminset.setVisible(true);
txtKey.setEnabled(false);
btnLogin.setEnabled(false);
}
else
{
button1.setEnabled(true);
txtname.setEnabled(false);
txtKey.setEnabled(false);
btnLogin.setEnabled(false);
}
}
//System.out.println(id+" in MainStego");
con.close();
break;
}
}
if(flag==0)
{
JOptionPane.showMessageDialog(this,"Invalid User & Key");
}
}
catch(Exception ex)
{
JOptionPane.showMessageDialog(this,"Run Time Error");

java.sql.SQLException: No suitable driver found for jdbc::mysql://localhost:3306/asd eroor [duplicate]

This question already has answers here:
The infamous java.sql.SQLException: No suitable driver found
(21 answers)
No suitable driver found for 'jdbc:mysql://localhost:3306/mysql [duplicate]
(8 answers)
Closed 5 years ago.
import java.sql.*;
class Mysqll{
public static void main(String args[]){
try{
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc::mysql://localhost:3306/asd","root","qwerty");
Statement st = con.createStatement();
ResultSet rs = st.executeQuery("select * from abc");
while(rs.next())
{
System.out.println(rs.getInt(1) + " " + rs.getString(2));
}
con.close();
}
catch(Exception e)
{
System.out.println(e);
}
}
}
I am new to jdbc programming. So plese help.
Getting this Exception while running the program.
java.sql.SQLException: No suitable driver found for jdbc::mysql://localhost:3306/asd
I had copied mysql-connector.jar file into jre/lib/ext folder.
Thanx in advance.
The connection string being passed to DriverManager is a URL. The formatting matters, and your problem is due to an extra colon between jdbc and mysql (verified by testing on my own system).
Replace jdbc::mysql: with jdbc:mysql:.

class.forname() giving the "driverclass" as exception [duplicate]

This question already has answers here:
Connect Java to a MySQL database
(14 answers)
Closed 8 years ago.
I used the following code segment for connecting my java application to mySQL DB, but its giving an error which I can't figure out why.
try {
con = null;
String url = "jdbc:mysql://localhost:3306/";
String db = "ass1";
String driver = "com.mysql.jdbc.Driver";
String user = "";
String pass = "";
System.out.println("OK 1"); //Checkpoint 1
Class.forName(driver).newInstance();
System.out.println("OK 2"); //Checkpoint 2
con = DriverManager.getConnection(url + db, user, pass);
System.out.println("OK 3"); //Checkpoint 3
}
catch (Exception e) {
System.out.println(e.getMessage());
}
Exception(Output):
run:
OK 1
com.mysql.jdbc.Driver
null
BUILD SUCCESSFUL(total time: 13 seconds)
So the exception is driver itself. It's the first time that I'm encountering this.
P.S.: Please ignore the checkpoints.
No Need to call newInstance() method .
Class.forName(driver);
is enough. Check that the required jar is available in your build path or not ?
Another Suggestion
Always try to use printStackTrace() instaed of getMessage()
You do not need to instantiate the driver, just call it's static initialization (e.g., by calling Class.forName) so it gets registered in the DriverManager:
System.out.println("OK 1"); //Checkpoint 1
Class.forName(driver); // no .newInstance() !
System.out.println("OK 2"); //Checkpoint 2
con = DriverManager.getConnection(url + db, user, pass);

How do I connect to access database using java at runtime? [duplicate]

This question already has an answer here:
Closed 10 years ago.
Possible Duplicate:
Accessing Access over JDBC (using ODBC?)
I have to do this since we have an unknown amount of access databases the user can select using our program so as to process data from them.
Here is connection code:
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver").newInstance();
String database = "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ=ACCESS_FILE_PATH/FILE_NAME.mdb";
connection = DriverManager.getConnection( database ,"username","password");
I did it as the following:
first , create a db DB1.MDB which contain a table named "table1";
second, config ODBC , create DatabaseSource named "Access2000"。
import java.sql.*;
class database {
public static void main(String args[]) {
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
String url="jdbc:odbc:Access2000";
Connection connection=DriverManager.getConnection(url);
Statement statement = connection.createStatement();
String sql="SELECT * FROM table1";
ResultSet rs = statement.executeQuery(sql);
String tt;
while (rs.next()) {
System.out.print("name:" + rs.getString("Name"));
System.out.println("age:" + rs.getString("Age"));
}
rs.close();
connection.close();
}
catch(Exception ex){
System.out.println(ex);
System.exit(0);
}
}
}

Categories

Resources