I can't seem to get a connection made on my Java program to java, I have started the MySQL Server, made a database in phpMyAdmin. But I'm confused on how I should use the JDBC Driver that I downloaded from MySQL.
Here's my code:
private void startConnection()
{
Connection conn = null;
String url = "jdbc:mysql://localhost/";
String dbName = "bank";
String driver = "com.mysql.jdbc.Driver";
String userName = "root";
String password = "password";
try {
Class.forName(driver).newInstance();
conn = DriverManager.getConnection(url+dbName,userName,password);
System.out.println("Connected to the database");
conn.close();
System.out.println("Disconnected from database");
} catch (Exception e) {
System.out.println("NO CONNECTION =(");
}
}
I had included the jar file in my JDK - jre\lib\ext folder but nothing. Any ideas?
Thanks in advance.
One thing stands out: you haven't specified a network port in the URL. The default port is 3306. So try:
jdbc:mysql://localhost:3306/
For the URL.
You have to specify the port. It's String url = "jdbc:mysql://localhost:3306/"; by default.
private void startConnection()
{
Connection conn = null;
String url = "jdbc:mysql://localhost:3306/";
String dbName = "bank";
String driver = "com.mysql.jdbc.Driver";
String userName = "root";
String password = "password";
try
{
Class.forName(driver).newInstance();
conn = DriverManager.getConnection(url+dbName,userName,password);
System.out.println("Connected to the database");
conn.close();
System.out.println("Disconnected from database");
}
catch (Exception e)
{
System.out.println("NO CONNECTION =(");
}
}
This will work
Related
I've been looking for verification of localhost(in my case XAMPP) username and password i.e 'root' and '' (null). Suppose user changed localhost username and password then how can we verify that using java + SQL QUERY.
//Database credentials
public static Connection conn = null;
public static Statement stmt = null;
public static String JDBC_DRIVER = "com.mysql.jdbc.Driver";
public static String DB_URL = "jdbc:mysql://localhost/";
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection(DB_URL);
System.out.println("Driver LOADED");
conn = DriverManager.getConnection( DB_URL , LH_USERNAME , LH_PASSWORD);
stmt = conn.createStatement();
Is there any way i can get Boolean result while executing one of the above
You can surround
conn = DriverManager.getConnection( DB_URL , LH_USERNAME , LH_PASSWORD);
whit try catch block in this way:
try {
conn = DriverManager.getConnection( DB_URL , LH_USERNAME , LH_PASSWORD);
}
catch(SQLException e){
if(e.getMessage().startsWith("Access denied for user")){
System.out.println("Error: nont valid user or password");
//you can add return false if you want
return false;
}
}
conn = DriverManager.getConnection( DB_URL , LH_USERNAME , LH_PASSWORD);
When this line executes if DB_URL , LH_USERNAME , LH_PASSWORD are not correct according to you database then you will get run time exception may SQLException . if some change then verify again then add following code
Connection conn = null;
try{
Class.forName("oracle.jdbc.driver.OracleDriver");
conn = DriverManager.getConnection(DB_URL , LH_USERNAME,LH_PASSWORD);
}catch(Exception e) {
e.printStackTrace();
}
if you get exception (conn==null) this means you are not able to connect with database. if you do not have correct DB_URL , LH_USERNAME , LH_PASSWORD then you will get run time exception when you try to execute following line
conn = DriverManager.getConnection(DB_URL , LH_USERNAME,LH_PASSWORD);
I am getting error when I try to connect database
Error:com.microsoft.sqlserver.jdbc.SQLServerConnection cannot be cast to
Ptakip.Connection
Ptakip is my Package
Connection is my Class
Here is the Connection Class Code ;
import java.sql.*;
public class Connection {
private Connection cn;
public Connection connector( )
{
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
Connection cn = (Connection)
DriverManager.getConnection("jdbc:sqlserver://localhost\\MyServer:
1433;databaseName=TEST;user=Glassfish;password=pass;");
System.out.println("connected");
}
catch(Exception ex) {
System.out.println("Error:" + ex.getMessage());
System.out.println(cn);
}
return cn;
}
}
your class has the same name as the class in the package java.sql that's why u have this conflict ,juste try to change the name to Connexion ,it should work
Try to make a connection like this. it may help you
String url = "jdbc:mysql://localhost:3306/";
String dbName = "demo”
String driver = "com.mysql.jdbc.Driver";
String userName = "root";
String password = "mypasswd";
try {
Class.forName(driver).newInstance();
Connection conn = DriverManager.getConnection(url+dbName,userName,password);
conn.close();
}catch (Exception e) {
e.printStackTrace();
}
}
}
I am trying to write a java program connecting to a MySQL database. But I am getting this error:
No suitable driver found for jdbc:mysql//localhost:3306/test
I have already installed mysql-connector-java.5.1.23-bin.jar. I am still getting this error.
Here is my program:
package database_practice;
import java.sql.*;
public class CreateConnection {
private Connection conn = null;
public static void main(String args[]) {
CreateConnection conn = new CreateConnection();
conn.getConnection();
}
public Connection getConnection() {
try {
String url = "jdbc:mysql//localhost:3306/test";
String username = "root";
String password = "admin1234";
Class.forName("com.mysql.jdbc.Driver").newInstance();
conn = DriverManager.getConnection(url, username, password);
System.out.println("Database Created...!!!");
}
catch (Exception e) {
e.printStackTrace();
System.out.println("Error occured while connecting to database");
}
return conn;
}
}
Your connection string is wrong. This:
"jdbc:mysql//localhost:3306/test"
should be:
"jdbc:mysql://localhost:3306/test"
Note the colon after "mysql".
i am using xampp mysql, this code is for JDBC program. actually there are two class one is dbconnect.java and another is login.java. I want to access the connection object (i.e. conn) in another class(i.e. login.java). But i don't have proper idea, i have included the code here please suggest me what is the problem and what are the solutions?
This is the code of dbconnect.java
package stundentrecord;
import java.sql.Connection;
import java.sql.DriverManager;
public class dbconnect {
public void conect(){
Connection con = null;
String url = "jdbc:mysql://localhost:3306/";
String db = "studentRecord";
String driver = "com.mysql.jdbc.Driver";
String user = "root";
String pass = "";
try{
Class.forName(driver);
con = DriverManager.getConnection(url + db, user, pass);
if(con==null){
System.out.println("Connection cannot be established");
}
// con.close();
} catch (Exception e) {
System.out.println(e);
}
}
}
and here is the code from another class named login.java
if(source==login){
if(username!=null && password!=null) {
Connection conn= null;
Statement stmt = null;
dbconnect db = new dbconnect();
db.conect();
String query = "SELECT * from userlogin";
try{
stmt=(Statement) conn.createStatement(); // here is the problem
ResultSet rs = stmt.executeQuery(query); // here is the problem
while (rs.next()) {
String user = rs.getString("username");
String pass=rs.getString("password");
System.out.println("Welcome "+user);
}
} catch(SQLException ex){
ex.getMessage();
}
StundentRecord SR = new StundentRecord();
} else {
JOptionPane.showMessageDialog(null,"Username or password field is empty","error !!",JOptionPane.ERROR_MESSAGE);
}
}
What is the real problem and how to solve it?
The easiest way would be to make the connect method non void and return the connection:
public Connection conect() {
Connection con = null;
String url = "jdbc:mysql://localhost:3306/";
String db = "studentRecord";
String driver = "com.mysql.jdbc.Driver";
String user = "root";
String pass = "";
try {
Class.forName(driver);
con = DriverManager.getConnection(url + db, user, pass);
if (con == null) {
System.out.println("Connection cannot be established");
}
return con;
} catch (Exception e) {
System.out.println(e);
}
return null;
}
You should return your CONNECTION object from you connection class and assign it to your login class... Now your connection object is null...
I've installed MySQL Connector J, WAMP [which comes with MySQL], and Java JDK 1.7 but it always through exception com.mysql.jdbc.Driver
The code
import java.sql.*;
public class sou
{
public static void main (String[] args)
{
Connection conn = null;
try
{
String userName = "root";
String password = "";
String url = "jdbc:mysql://localhost/cms";
Class.forName ("com.mysql.jdbc.Driver").newInstance ();
conn = DriverManager.getConnection (url, userName, password);
conn.close();
}
catch (Exception e)
{
System.err.println (e.getMessage());
}
}
}
I'm compiling this as
C:\Users\Sou\Desktop>javac -cp "D:\Program Files\MySQL\Connector J 5.1.20.0\mysql-connector-java-5.1.20-bin.jar" sou.java
You have to include mysql-connector-java-5.1.20-bin.jar in your CLASSPATH while compiling and running an application.
C:\Users\Sou\Desktop>javac -cp .;"D:\Program Files\MySQL\Connector J 5.1.20.0\
mysql-connector-java-5.1.20-bin.jar" sou.java
C:\Users\Sou\Desktop>java -cp .;"D:\Program Files\MySQL\Connector J 5.1.20.0\
mysql-connector-java-5.1.20-bin.jar" sou
And no need to call the newInstance() method.
Connection conn = null;
try{
String userName = "root";
String password = "";
String url = "jdbc:mysql://localhost/cms";
Class.forName ("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection (url, userName, password);
}
catch (Exception e){
System.err.println (e.getMessage());
}finally{
if(conn!=null){
try{
conn.close();
}catch(Exception ex) { }
}
}
You can try this :
DriverManager.registerDriver(new com.mysql.jdbc.Driver());
instead of
Class.forName ("com.mysql.jdbc.Driver").newInstance ();
Insert this code in your program to check for classpath
System.out.println(System.getProperty("java.class.path"));