Getting number format exception when accessing mysql from JDBC remotely - java

I can access my remote database from the command line with users I create but when i try to connect from jdbc i get number format exception.
public static final String URL = "jdbc:mysql://ipv6:3306/Library";
public static final String USER = "user";
public static final String PASSWORD = "correctpass";
conn = DriverManager.getConnection(url, user, password);
throws
com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Cannot load connection class because of underlying exception: 'java.lang.NumberFormatException: For input string: "30a:2e1a:2470:7c29:300b:f757:2941:3306"'
I can login throw the terminal with the same credentials thats why im so confused. Thanks for the help.

Not sure why you would use IPV6 address to connect but it can be done.
public static final String urlString = “jdbc:mysql://address=(protocol=tcp)(host=your-ipv6-address)(port=3306)/Library”;
Class.forName(driver);
public static final String user= "user";
public static final String password= "correctpass";
DriverManager.setLoginTimeout(getConnectionTimeOut());
dbConnection = DriverManager.getConnection(urlString,user,password);
Try above. Due to the reading address from mysql connection it was expect to have port read after the colon, and all ports are in numbers. Since you are not using standard address it would return you error as it tries to parse the port (all numbers as expected - but has letters so it hits number format exception). If you are using IPV6 you need to declare it as IPV6.

Related

Hello all. I am trying to connect my Java program to mysql database. My aim of program is "ADD 2 VALUE FROM SERVLET TO DATABASE." [duplicate]

I am trying to write code for bringing a text file's data into a database using Eclipse, MySQL Workbench, and JDBC 8.0.11. It is giving me a ClassNotFoundException. I have looked at multiple other questions, and they have all been fixed by putting the java\com\mysql\jdbc\Driver.java inside the DriverManager.getConnection parameter. I have already done that, and it is still giving me an error. Anyone have any ideas as to why I'm still getting this error?
public static void main(String[] args) throws Exception{
Class.forName //Register JDBC Driver
("*mysql-connector-java-8.0.11.\\src\\legacy\\java\\com\\mysql\\jdbc\\Driver.java*")
.newInstance();
conn = DriverManager.getConnection (url, user, pass);
Statement stmt = conn.createStatement();
String mysql1 = "UPDATE Policy SET " + readAndArray //Changeable file path
("filepath");
}
NEW EDIT
Following #zlakad 's advice, it turns out that you don't need to use Class.forName() if you have Java 6 or higher. Although, now I have a new error: SQLNonTransientConnectionException because of the underlying WrongArgumentException. I'm puzzled as to why it does this because I'm not using the incorrect parameters for DriverManager.getConnection. Any suggestions?
String url = "file path"; //Changeable for MySQL DB
String user = "root";
String pass = "password";
public static void getConnection() throws Exception {
Connection conn = DriverManager.getConnection(url, user, pass);
Statement stmt = conn.createStatement();
Try this:
// None of this belongs in a main method.
public static void main(String[] args) throws Exception {
Class.forName("com.mysql.jdbc.Driver");
// where are url, user, pass set? I don't see them.
Connection conn = DriverManager.getConnection(url, user, pass);
Statement stmt = conn.createStatement();
// this is simply wrong.
String mysql1 = "UPDATE Policy SET " + readAndArray("filepath");
}
You're new to Java and JDBC. This is not a good way to do it. I'd recommend that you search the web and SO for some examples of how to do it better.
You have to load driver class for connection not jar file of that class
you shoud try this:
Class.forName("com.mysql.jdbc.Driver");
I was using the wrong format for a database url in the DriverManager.getConnection();
I changed my url to a jdbc:mysql://host:3306/ and it worked.
String url = "jdbc:mysql://*host*:3306/";
Connection conn = DriverManager.getConnection(url, user, pass);

Connect to MySQL with Java - JDBC without showing credentials in Java source code

I am trying to learn how you would tackle the task of creating a Java console application, connect to a (in this case) MySQL DB and send or retrieve data, without showing your username and password in the source code of the Java application. I currently have no trouble
creating a connection showing credentials.
// JDBC driver name and database URL
private static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
private static final String DB_URL = "jdbc:mysql://192.168.1.159:3306/javahelper";
// Database credentials
private static final String USER = "xxxx";
private static final String PASS = "RandomString";
/**
* #return
*/
public Connection openConnection() {
Connection connection = null;
try {
Class.forName(JDBC_DRIVER);
// opening connection
connection = (Connection) DriverManager.getConnection(DB_URL,USER,PASS);
} catch (ClassNotFoundException e) {
System.out.println("This is from openConnection method");
e.printStackTrace();
} catch (SQLException f) {
System.out.println("This is from openConnection method");
f.printStackTrace();
}
return connection;
}
From what information I can gather you always need to show your credentials somewhere in the application. But how do you than achieve "safe" connection between a application and a DB, so others can't misuse your credentials for malicious reasons?
one way of doing it is using a properties file having your credentials or having your data in a xml file.
create a properties file like the one below
// database.properties
DB_URL=jdbc:mysql://localhost:3306/UserDB
DB_USERNAME=user_name
DB_PASSWORD=password
Use this information in your code to get the username and passwords.
Properties properties= new Properties();
FileInputStream input = null;
try{
input = new FileInputStream("database.properties");
props.load(input );
con = DriverManager.getConnection(props.getProperty("DB_URL"),props.getProperty("DB_USERNAME"),props.getProperty("DB_PASSWORD"));
}
you can use encrypt the username and password.The best opensource encryptor(My personal view) is jbcrypt
// Hash a password for the first time
String hashed = BCrypt.hashpw(password, BCrypt.gensalt());
// gensalt's log_rounds parameter determines the complexity
// the work factor is 2**log_rounds, and the default is 10
String hashed = BCrypt.hashpw(password, BCrypt.gensalt(12));
// Check that an unencrypted password matches one that has
// previously been hashed
if (BCrypt.checkpw(candidate, hashed))
System.out.println("It matches");
else
System.out.println("It does not match");
Sharing what i find
Creating and using the propertise file
I created a database.properties file(normal text file) and placed it in the src folder of the Java project.
JDBC_DRIVER=com.mysql.jdbc.Driver
USER=YourUser
PASS=YourPassword
DB_URL=jdbc:mysql://IP:PORT/DB
Afterwards i edited my openConnection() method to use the properties file for loading the credientials of the connection.
public Connection openConnection() {
Properties properties = new Properties();
Connection connection = null;
String path = System.getProperty("user.dir");
path += "/src/database.properties";
try(FileInputStream fin = new FileInputStream(path);) {
properties.load(fin);
try {
Class.forName(properties.getProperty("JDBC_DRIVER"));
// opening connection
connection = (Connection) DriverManager.getConnection(properties.getProperty("DB_URL"),properties.getProperty("USER"),properties.getProperty("PASS"));
} catch (ClassNotFoundException e) {
System.out.println("This is from openConnection method");
e.printStackTrace();
} catch (SQLException f) {
System.out.println("This is from openConnection method");
f.printStackTrace();
}
} catch (IOException io) {
System.out.println("This is from openConnection method");
io.printStackTrace();
}
return connection;
}
Sending username and password, Java application -> MySQL
From what i can read on the web, it dosent matter much if you encrypt or hash the password before you send it towards the sequel service from your Java application. An example i found is that the sequel service dosent have a "receive hash method and authenticate". And even if it did the hash would need to be in the program somewhere. And when the program has access to it, others also have access to it if they really want it. Also if the hash is whats needed to authenticate than your back to where you can just as well use the clear text password.
The discussion than ends on "what is the best approach". Some suggest a keyserver / auth system in between the application and sequel service, using a datastore setup on the server side, using the OS "wallet" (example Windows registry) or creating a database user with minimum permissions to just get the job done / or a read only DB "read_only=1 in my.cnf".
I tried the 3'rd option and created a "DBaccess" user, with only the select permission to retrieve data, no administrative rights and random generated password by MySQL.

Trying to connect to my sql database getting exception saying "no suitable driver found"

I'm using the tutorial at http://www.vogella.com/tutorials/MySQLJava/article.html
to try tp connect to my sql server on my server
When it executes the line:
Connection connect = DriverManager
.getConnection("jdbc:mysql:http://www.findmeontheweb.biz"
+ "user=findmeon_bitcoin&password=PASSWORD");
an exception gets thrown saying "No sutabled driver found for jdbc:mysql:http://www.findmeontheweb.biz
This is what I did
1. Downloaded the "mysql-connecter-java-5.1.33.bin.jar into my lib folder
2. added the jar to my project from preferences.
project code:
public class cStart {
private Connection connect = null;
private Statement statement = null;
private PreparedStatement preparedStatement = null;
private ResultSet resultSet = null;
public static void main (String[] args) {
int g=0;
try {
// this will load the MySQL driver, each DB has its own driver
Class.forName("com.mysql.jdbc.Driver");
// setup the connection with the DB.
// EXCEPTION GOES OF HEAR
Connection connect = DriverManager
.getConnection("jdbc:mysql:http://www.findmeontheweb.biz"
+ "user=findmeon_bitcoin&password=PASSWORD");
} catch (Exception e) {
System.out.println("Exception...." );
}
}
}
The URL format should be look like this
jdbc:mysql://hostname/ databaseName
I think this is a much cleaner way to do it:
String URL = "jdbc:URL_TO_YOUR_DATBASE";
String USER = "username";
String PASS = "password"
Connection conn = DriverManager.getConnection(URL, USER, PASS);
As seen here: http://www.tutorialspoint.com/jdbc/jdbc-db-connections.htm
I say give that link a try with your driver. You also should make sure you have the actual jar for MySQL. It really might be invalid.
I would try the one here: http://www.java2s.com/Code/Jar/c/Downloadcommysqljdbc515jar.htm
And then add that to your project.
The URL to the database might be wrong.
If yes you should specify a correct one with including database name.
Else verify if the jdbc driver jar is added in the build-path, if yes try to put it in the lib folder of your webapp.

can't connect to MySQL on localhost

i am trying to connect to Mysql database using the code below , yet my attempt fails.
this is my attempt:
private static Connection conn = null;
private static String url = "jdbc:mysql://localhost/";
private static String dbName = "proj1";
private static String driver = "com.mysql.jdbc.Driver";
private static String userName = "root";
private static String password = "root";
public static int setupConnection ()
{
try{
Class.forName(driver).newInstance();
conn = DriverManager.getConnection(url+dbName,"root","root");
return 1;
}
catch (Exception e)
{
JOptionPane.showMessageDialog(null, e.getMessage());
return 0;
}
}
when installing MySQL i remember entering the password "root" , but im not 100% sure if the username is autmatically assigned "root" , i really appreciate your help.
i get the error message : com.mysql.jdbc.Driver
You need to add the MySQL Connector/J driver to the build-path/classpath of your Netbeans project. Otherwise it cannot be loaded.
Unfortunately you did not mention what kind of failure you got.
But here are some tips.
My JDBC URL looks like the following. jdbc:mysql://localhost:3306/MYSCHEMA. So port and schema name are missing in yours.
To check your credentials try to connect to your DB using command line client:
mysql -uroot -proot
Read the error message if you fail. If you cannot restore credentials, re-install MySql. It takes 3 minutes. Do not try to connect to DB using your code unless you can do it using existing clients.
Good luck.
you should connect to port address 3306,
change the url as below :
private static String url = "jdbc:mysql://localhost:3306/";
I am considering that you are not getting any compilation error and you have added mysql java api..
Start by trying to log into mysql from a command shell. If you can't, JDBC won't be able to, either.
This might help if you can't remember:
http://www.cyberciti.biz/tips/recover-mysql-root-password.html

Java Connect to database Error: java.lang.ClassNotFoundException: com.mysql.jdbc.Driver

Hey stupid question but I'm having a hard time connecting my java program to a mysql database.
Throwing an exception when I hit this line.
Class.forName(driverName).newInstance();
The driver name is com.mysql.jdbc.Driver. I've searched around a bit on google and found something about a mysql-connector.jar file that I'm apparently supposed to have but I really haven't looked into it yet. Thanks.
Entire code:
Connection connection = null;
try
{
String driverName = "com.mysql.jdbc.Driver"; // MySQL MM JDBC driver
Class.forName(driverName).newInstance();
String serverName = "*********";
String database = "canteen_web3";
String url = "jdbc:mysql://" + serverName + "/" + database;
final String username = "*****";
final String password = "******";
connection = DriverManager.getConnection(url,username,password);
System.out.println("Connected!");
}
catch(Exception ex)
{
throw new ICException(ex.getMessage());
}
Start your app with
java -classpath .:mysql-connector.jar MyClass
The colon separates two paths. The . is the directory you are in (and hopefully the class or the base package), the latter jar is the driver.
For further information refer to the various sources of documentation http://download.oracle.com/javase/1.5.0/docs/tooldocs/windows/classpath.html

Categories

Resources