I'm writing an app, which connects to the database, and sends a basic query. Till now I did all my tests on Android Studio Emulator and it worked fine, until I tried it on my phone, which can't connect to the database.
try{
String myUrl = "jdbc:mysql://192.168.0.12/mydb";
miastoItemList =new ArrayList<>();
Connection c = DriverManager.getConnection(myUrl, "root",
"test");
Statement statement = c.createStatement();
String s = "SELECT * FROM city";
ResultSet rs = statement.executeQuery(s);
}
Related
I am new to google cloud, and I just configure all components needed in cloud computation. But I have a problem on MySQL connection between MySQL and APP engine/VM. I can browser see my main html page, but when my servlet try to make MySQL connection and it failed.
Class.forName("com.mysql.cj.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql://mysql external IP:3306/Database?user=us&password=pw");
Error message: com.mysql.cj.jdbc.exceptions.CommunicationsException: Communications link failure and The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
Class.forName("com.mysql.jdbc.GoogleDriver");
con = DriverManager.getConnection("jdbc:google:mysql://mysql external IP:3306/Database?user=us&password=pw");
Error message: java.lang.ClassNotFoundException: com.mysql.jdbc.GoogleDriver at java.net.URLClassLoader.findClass(URLClassLoader.java:382)
I add appengine-web.xml in WEB-INF folder with true
Does anyone have the solution on this? I deploy to google cloud from my local Eclipse 2020-06 version.
Thanks
You an try this sample code:
String url = "jdbc:mysql://ip:3306/dbname?useSSL=false";
String user = "root";
String password = "password";
StringBuffer datStr = new StringBuffer("");
String query = "SELECT * FROM dbname.tablename";
try {
Class.forName("com.mysql.cj.jdbc.Driver");
Connection con = DriverManager.getConnection(url, user, password);
Statement st = con.createStatement();
ResultSet rs = st.executeQuery(query);
while (rs.next()) {
//Code
}
} catch (Exception ex) {
ex.printStackTrace();
}
I have been writing an android application in android studio that needs access to a cloud database. I am using Microsoft Azure for my database and have already created it, however I am struggling to query it from the android application. When trying from Android Studio, I get an error to do with JDBC not being found, however I have tried adding the relevant .jar files with no luck. I have also tried creating a maven project as suggested on the Azure site, and with this I can access the database fine, however as soon as I try using the package created by maven as a module in Android Studio, the sql query no longer works and instead goes to the exception. Below you can see the function I am trying to access in the maven project:
public static ArrayList<Double[]> getIncidentDetails() {
// Connect to database
String url = "jdbc:sqlserver://............"; //Take ............ to be my connection string
Connection connection = null;
ArrayList<Double[]> results = new ArrayList<Double[]>();
try {
connection = DriverManager.getConnection(url);
System.out.println("Connected");
String selectSQL = "SELECT [latitude], [longitude]" + "FROM [Incidents]";
try (Statement statement = connection.createStatement();
ResultSet resultSet = statement.executeQuery(selectSQL)) {
while (resultSet.next()) {
System.out.println(resultSet.getString(2));
Double latitude = Double.parseDouble(resultSet.getString(1));
Double longitude = Double.parseDouble(resultSet.getString(2));
results.add(new Double[] {latitude, longitude});
}
connection.close();
}
} catch (Exception e){
System.out.println(e.getMessage());
}
return results;
}
Any ideas on how I could connect to the database from Android Studio, with or without the Maven project would be greatly appreciated.
Try to load the class before you open the connection:
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
con = DriverManager.getConnection(connectionUrl);
I am trying to create a MySQL DB for my program to connect to. I have ensured that Maven's library was added properly.
I'd like my program to create the database reviews if it's not created and if it isn't created then do I need to make sure that it connects without running the program all over again.
My current code is as follows:
String url = "jdbc:mysql://localhost:3306";
String user = "root";
String password = "pass";
try {
Connection conn = DriverManager.getConnection(url,user,password);
Statement stmt = conn.createStatement();
int result = stmt.executeUpdate("CREATE DATABASE IF NOT EXISTS reviews");
I get this error:
java.sql.SQLSyntaxErrorException: Table 'reviews.reviews' doesn't exist
Now, if I change code to:
String url = "jdbc:mysql://localhost:3306";
String user = "root";
String password = "pass";
try {
Connection conn = DriverManager.getConnection(url,user,password);
Statement stmt = conn.createStatement();
int result = stmt.executeUpdate("CREATE DATABASE reviews");
I get this error:
java.sql.SQLException: Can't create database 'reviews'; database exists
I'm creating a website for the first time all by myself. I'm doing the logic in Java (I use Ubuntu), and I have written the SQL script for the data base on windows using pgAdmin3 (postgresql).
How do I connect my java program to its database?
If its just java and mysql, then you can try something like this
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost/databaseName", "username","password");
As in the example below
public boolean isIdInDb(String id) throws SQLException{
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost/databaseName", "username","password");
String rawInfoDb = "select user_id from users ";
Statement statement = conn.createStatement();
ResultSet rSet = statement.executeQuery(rawInfoDb);
boolean isIt = false;
while(rSet.next()){
if(id.equals(rSet.getString(1))){
isIt = true;
}
}
return isIt;
}
If I am using the below code query the database, where would I find the log file for the process? I am looking for what is being sent to the database. The query works from SQL Server Management Studio. The database is MS-SQL 2008.
try{
Class.forName("net.sourceforge.jtds.jdbc.Driver");
String userName = "dbuser";
String password = "dbpswd";
String url = "jdbc:jtds:sqlserver://server:1043"+";databaseName=databasename";
Connection con = DriverManager.getConnection(url,userName,password);
Statement stm = con.createStatement();
ResultSet rs = stm.executeQuery
("SELECT users.id,users.role FROM users WHERE users.username = 'xusername' AND users.password = 'xpswd' AND users.active = 1");
}
When the code runs it returns
'No current row in the ResultSet.'
If I use
System.out.println(stm);
it returns
net.sourceforge.jtds.jdbc.JtdsStatement#7fd2c698
You have to use
rs.next()
to retrieve the first entry. A ResultSet cursor is initially positioned before the first row.