Connect mysql with java - java

I am trying to connect to mysql in java (I use Wamp) so I used the following code : (I'm using Eclipse)
package genererPlanning;
import java.sql.*;
public class genererPlanning{
static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
static final String DB_URL = "jdbc:mysql://localhost/mysql";
static final String USER = "root";
static final String PASS = "";
public static void main(String[] args){
Connection conn = null;
try {
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/mysql","root", "");
}
catch (SQLException ex){
System.out.println("SQLException: " + ex.getMessage());
System.out.println("SQLState: " + ex.getSQLState());
System.out.println("VendorError: " + ex.getErrorCode());
}
}
}
But it returned me this text :
SQLException: No suitable driver found for jdbc:mysql://localhost:3306/mysql
SQLState: 08001
VendorError: 0
Do you know why it does not work?
PS : I looked at theses links but don't find my answer :
- stackoverflow
- stackoverflow
- stackoverflow
- commentcamarche
Thanks for all.

You need to use the mysql-connector.jar
Here's the link with instructions as to how to set it to your classpath.
http://code.google.com/p/find-ur-pal/downloads/detail?name=mysql-connector-java-5.1.18-bin.jar&

You need mysql jdbc connector..download from this site

Related

I have problems with the connection of my embedded derby database, What's the problem?

I was making this java application, it works fine in Netbeans but when I tried to use the .jar out of Netbeans the app doesn't load the db. I'm using the derby.jar to make the db. Also the main problem it's the connection class in the public Connection CargarDB()
I'm tried changing my URL and surrounding all the block with a Try...Catch and it's not working yet.
public Connection CargarDB(){
Connection con;
String barra = File.separator;
String prjt = System.getProperty("user.dir") + barra + "Registros";
try {
Class.forName("org.apache.derby.jdbc.EmbeddedDriver");
String db = "jdbc:derby:" + prjt;
con = DriverManager.getConnection(db);
System.out.println("Base de datos cargada");
return con;
} catch (ClassNotFoundException | SQLException ex) {
System.out.println("Error " + ex);
}
return null;
}
It doesn't give an error in Netbeans but in the derby document in the dist folder show me this:
-java.sql.SQLException: Database 'C:\Users\huevo\Documents\JAVA\Proyecto_T\dist\Registros' not found.
-Caused by: ERROR XJ004: Database 'C:\Users\huevo\Documents\JAVA\Proyecto_T\dist\Registros' not found.
I expect the output of my db show me the results in my JTable but it doesn't show anything

Java - Unable to connect to MySQL Database from Cpanel (godaddy). (Error: java.sql.SQLException: Access denied for user ')

I am unable to connect to the database on my website. The user account has permissions and the user name and password are correct. The only thing that could be wrong is the link I am using for the database because I am just doing "http://" as my link. I am assuming the database is there. Godaddy cPanel portal will not show me where it directly is at.
I do not want to do localhost, I want to connect from Java to an actual database on my cPanel.
NOT actual credentials.
In output it says:
run: Error: java.sql.SQLException: Access denied for user 'usertest'#'test.com' (using password: YES) java.lang.NullPointerException BUILD SUCCESSFUL (total time: 0 seconds)
Code:
Main:
package main;
public class main {
public static void main(String[] args) {
db connect = new db();
connect.getData();
}
}
db:
package main;
import java.sql.*;
public class db {
private Connection con;
private Statement st;
private ResultSet rs;
public db(){
try{
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql://","usertest","test1");
st = con.createStatement();
}catch(Exception ex){
System.out.println("Error: "+ex);
}
}
public void getData() {
try{
String query = "select * from persons";
rs = st.executeQuery(query);
System.out.println("Records from Database");
while(rs.next());
String name= rs.getString("name");
String age = rs.getString("age");
System.out.println("Name: "+name+" "+"Age"+age);
}catch(Exception ex){
System.out.println(ex);
}
}
}
Go the the cpanel "mysql remote" option and allow you ip to connect to mysql.
Found out how to connect, I just did a % only wildcard and it worked. I tried using my IP adresses and IP adresses with wildcards but only a % seemed to work for now.

Getting "Unauthorized" exception when trying to create connection in Arango database using Java

I am trying to create a connection in Arango databse using Java , IDE- Eclipse but while executing the program I am getting the exception "Unauthorized".
Note: I have logged in to Arango Database with user :root
URL- http://127.0.0.1:8529/_db/_system/_admin/aardvark/index.html#logs
Program in Eclipse
import static com.arangodb.*;
public class FirstProject {
public static void main(String[] args) {
ArangoConfigure configure = new ArangoConfigure();
configure.init();
ArangoDriver arangoDriver = new ArangoDriver(configure);
String dbName = "mydb";
try {
arangoDriver.createDatabase(dbName);
System.out.println("Database created: " + dbName);
} catch (Exception e) {
System.out.println("Failed to create database " + dbName + "; " + e.getMessage());
}
}
}
I have used the tutorial https://www.arangodb.com/tutorials/tutorial-java-old/ to write this program and followed all the steps mentioned. Still getting unauthorized exception:
Failed to create database mydb; Unauthorized
You have to set the user and password (default user "root", password empty):
ArangoConfigure configure = new ArangoConfigure();
configure.init();
configure.setUser("root");
configure.setPassword("");
ArangoDriver arangoDriver = new ArangoDriver(configure);
In addition I highly suggest you to update to the new java driver (version 4.1.12). Which comes with a new API and a better performance). There is also a tutorial for it (see here).
The required code for your problem in this version would be:
ArangoDB arangoDB = ArangoDB.Builder().user("root").password("").build();

Setting up a remote Derby DB: "No suitable driver found" error

I am trying to set up a remote Derby database just for practice. The following code works without a problem whenever I access the DB on my harddrive:
class Test{
public static void main(String[] args) {
String protocol = "jdbc:derby:";
// String dbPath = "C:/Java_Practice/derbyDB"; // this dbPath works...
String dbPath = "//108.167.141.127/derbyDB"; // and this one doesn't
String url = protocol + dbPath;
try( Connection conn = DriverManager.getConnection(url) )
{
System.out.println(conn);
}
catch(SQLException e){
System.out.println(e.getMessage());
}
}
}
I then uploaded the whole derbyDB directory to my Hostgator-hosted website, obtained its IP by pinging the server and edited the dbPath var accordingly. The code stopped working as if it can't even see the DB. What am I missing?
Looks like your driver class not loaded.
Try loading it before calling DriverManager.getConnection, and see if it works.
String driver = "org.apache.derby.jdbc.ClientDriver";
Class.forName(driver).newInstance();
String protocol = "jdbc:derby:";
String dbPath = "//108.167.141.127/derbyDB"+";create=true";
String url = protocol + dbPath;
Connection conn = DriverManager.getConnection(url);
Answering my own question after some digging.
This is what I found in the official Apache Derby documentation (https://db.apache.org/derby/docs/10.0/manuals/develop/develop14.html):
You can specify only databases that are local to the machine on which
the JVM is running.
Looks like what I wanted to accomplish cannot be done...

Java jTDS Connection Problems Ubuntu Server

From what I have read the correct connection string for a jTDS is:
jdbc:jtds:<server_type>://<server>[:<port>][/<database>]
I believe the issue is the server name. The server name is formatted like this
servername\adhoc
An SQLException gets thrown anytime I try to connect saying "unknown server host name"
Is that my issue, or is there something else I need to look into as well...?
import java.sql.*;
public class Main {
// The JDBC Connector Class.
private static final String MSdbClassName = "net.sourceforge.jtds.jdbc.Driver";
private static final String MSHOST = "servername\\adhoc"; //cascrmeufosqlp1\adhoc
private static final String MSDATABASE = "tier2";
private static final String MSUSER = "feed_****2";
private static final String MSPASSWORD = "*******0";
public static void main(String[] args) throws ClassNotFoundException,SQLException
{
Class.forName(MSdbClassName);
String url2 = "jdbc:jtds:sqlserver://" + MSHOST + ":1433/" + MSDATABASE;
Connection c2 = java.sql.DriverManager.getConnection( url2, MSUSER, MSPASSWORD );
System.out.println("MS SQL works...");
c2.close();
}
}
It looks like you are trying to connect to a "named instance" of sql server. You will need to use the "instance" property in the url. Something like this might work:
jdbc:jtds:sqlserver://servername:1433/dbName;appName=MyAPP;instance=instanceName
See the jTDS faq for more information found here: http://jtds.sourceforge.net/faq.html

Categories

Resources