I m trying to create a dynamic website where I can get data from the database and output it to the user.
I created the connection between MySQL workbench to Google cloud app engine, and I run on the cloud shell to test the database, it works.
But when I try to change the URL on eclipse, it just cannot connect to the server, and gives me errors "Failed to load resource: the server responded with a status of 500 ()".
the URL sting are:
String url = "jdbc:mysql:///film?cloudSqlInstance="
+ "causal-relic-333309:europe-west6:jiayaodb="
+ "&socketFactory = com.google.cloud.sql.mysql.SocketFactory"
+ "&user=root"
+ "&password=password";
private Connection openConnection(){
// loading jdbc driver for mysql
try{
Class.forName("com.mysql.jdbc.Driver");
} catch(Exception e) { System.out.println(e); }
// connecting to database
try{
// connection string for demos database, username demos, password demos
conn = DriverManager.getConnection(url);
stmt = conn.createStatement();
} catch(SQLException se) { System.out.println(se); }
return conn;
}
So I think is the connection error where I can't get it to connect to Google Cloud SQL.
you need to use Cloud SQL proxy to connect to the database from your local machine.
Related
I'm trying to open an sqlite jdbc connection but I keep getting an exception
java.sql.SQLException: opening db:
'\data\data\com.lppapp.ioi.lpp\databases\lppDB.db': Read-only file
system
Before trying to get the jdbc connection I copy the database into the internal storage and try to access it from there. I'm using this code to connect:
public Connection connect() {
String url = "jdbc:sqlite:\\data\\data\\com.lppapp.ioi.lpp\\databases\\lppDB.db";
Connection conn = null;
try {
Properties config = new Properties();
config.setProperty("open_mode", "1");
//TODO: can't open db (read-only file system) even though directory has a+rw permissions
conn = DriverManager.getConnection(url, config);
System.out.println("Connection successful.");
} catch (SQLException e) {
System.out.println("Connection failed.");
System.out.println(e.getMessage());
}
return conn;
}
I'm using an Android emulator and I've checked the permissions of the databases directory.
I've tried every solution found on the web but with no help.
I've decided to use Android's native SQLite API as mentioned by CommonsWare.
Thanks!
I'm trying to connect MySQL by passing an IP address as a parameter instead of localhost.
Class.forName("com.mysql.jdbc.Driver");
try{
String url = "jdbc:mysql://192.168.1.116:3306/db";
DBConnection = DriverManager.getConnection(url, "root", "");
}
catch(Exception ex){ ex.printStackTrace(); }
The problem is the connection takes 1-2 minutes before showing this error.
com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure
By the way, I'm using XAMPP.
I'm trying to connect my android app with an online database server that uses MySQL and phpMyAdmin.
But problem I am having is that I'm unable to access my database (which is online) using Java code for android.
It get the following error:
java.sql.SQLException: No suitable driver at java.sql.DriverManager.getConnection(DriverManager.java:187)
I already loaded the driver and external JAR of MySQL but it is still showing this exception.
The code is :
Class.forName("com.mysql.jdbc.Driver").newInstance();
Connection con = DriverManager.getConnection(
"jdbc:mysql:http://www.demo.com:2082/",
"usernme", "password"); //username and password of cpanel access
PreparedStatement statement = con
.prepareStatement("SELECT * FROM users WHERE userName = '"
+ userName + "'");
ResultSet result = statement.executeQuery();
while (result.next()) {
retrievedUserName = result.getString("username");
Log.e("data username:", retrievedUserName);
retrievedPassword = result.getString("password");
}
Use the Webservice to make the Connection to Mysql from your android app through json or xml.you can use SQlite database to connect the android app internally by the use of this package android.database.sqlite classes
I am trying to connect to SQL Server 2008 via Java.
I've added sqljdbc4.jar to my project's library.
No username and password is set for database accessing the database (Windows Authentication).
The 1433 port is Listening, but I still receive this exception:
SQL Exception: com.microsoft.sqlserver.jdbc.SQLServerException: Login failed for user ''. ClientConnectionId:085d5df3-ad69-49e1-ba32-b2b990c16a69
Relevant code:
public class DataBases
{
private Connection link;
private java.sql.Statement stmt;
public ResultSet rs;
public DataBases()
{
try
{
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
String connectionUrl = "jdbc:sqlserver://localhost:1433;databaseName=DB;";
Connection con = DriverManager.getConnection(connectionUrl);
}
catch (SQLException e)
{
System.out.println("SQL Exception: "+ e.toString());
}
catch (ClassNotFoundException cE)
{
System.out.println("Class Not Found Exception: "+ cE.toString());
}
}
}
If you want windows authentication you need to add the option integratedSecurity=true to your JDBC URL:
jdbc:sqlserver://localhost:1433;databaseName=DB;integratedSecurity=true
You also need sqljdbc_auth.dll (beware of 32/64 bit) in your Windows system path or in a directory defined through java.library.path
For details see the driver's manual: http://msdn.microsoft.com/en-us/library/ms378428.aspx#Connectingintegrated
I was having same issue when I tried to connect to Microsoft SQL server from Java. I used jTDS driver instead of regular SQLJdbdc Driver.
Class.forName("net.sourceforge.jtds.jdbc.Driver");
String connectionUrl = "jdbc:jtds:sqlserver://localhost:1433;databaseName=DB;integratedSecurity=true";
Connection con = DriverManager.getConnection(connectionUrl);
I had the same issue. Its because of the wrong format of the ConnectionUrl. You are missing username and password in the ConnectionUrl.
String ConnectionUrl = "jdbc:sqlserver://localhost:1433;databaseName=DB","username","password");"
I hope it works well!!!
This post my help:
jdbc.SQLServerException: Login failed for user for any user
You need the integratedSecurity=true flag and make sure the server properties are indeed set to 'Windows Authentication Mode' under Security.
I am new and any help will be highly appreciated. I have a Webapp installed on Amazon EC2. I have installed Tomcat7 and MySQL 5.5 on Amazon EC2.
I am using following code in servlet for JDBC-MySQL connection
Connection connection;
Statement statement;
ResultSet rs = null;
try {
// load Connector/J
Class.forName("com.mysql.jdbc.Driver");
connection = DriverManager.getConnection(JDBC_MYSQL_STRING);
System.out.println(TAG + " Connection with MySQL established");
statement = connection.createStatement();
....
....
....
rs.close();
statement.close();
connection.close();
} catch (ClassNotFoundException ex) {
Log.log(Level.SEVERE, "ClassNotFoundException", ex);
} catch (SQLException e) {
for (Throwable t : e) {
System.out.println(t.getMessage());
}
I have included mysql-connector-java-5.1.13-bin.jar for Connector/J in the Library folder for Netabeans and deployed war files on tomcat7 in Amazon ec2.
My Question
I have learned that JDBC-MySQL connection is very time consuming operation and connection pools can be used to keep live connection which will reduce time consumed in connection. I am new to this and have read my blogs but unable to understand how to set up connection pool and how to use it in the servlets?
Thanks.
You need to setup the DB Connection in server.xml
Follow this:
http://www.mulesoft.com/tomcat-mysql
The URL of your database would take this form:
"jdbc:mysql://yourdatabasename.foo.us-east-1.rds.amazonaws.com:3306/"