I use PostgreSQL to create my database and save my users list to it, when i try to connect db by java jdbc, i get error that say:
"java.sql.SQLException: invalid database address:
jdbc:postgresql://localhost:5432/users".
i use "JDBC41 Postgresql Driver, Version 9.3-1102" from PostgreSQL website.
and this is my code:
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class javaconnect {
private static Connection c = null;
public static Connection connectDb() {
try {
Class.forName("org.postgresql.Driver");
c = DriverManager.getConnection("jdbc:postgresql://localhost:5432/users", "postgres", "12345");
return c;
} catch (ClassNotFoundException | SQLException e) {
System.err.println(e.getClass().getName() + ": " + e.getMessage());
System.exit(0);
return null;
}
}
}
Thanks.
As the error,
"java.sql.SQLException: invalid database address:
says your database name is incorrect . check for the database name if you have something like sql developer installed.
Valid database name should be specified here "jdbc:postgresql://localhost:5432/users" after the /localhost:5432/
Read JDBC using postgresql to connect to PostgreSQL database using jdbc
Related
I'm trying to connect from SSMS(SQL Server Management Studio) to Eclipse(Java), but I keep getting this error message:
com.microsoft.sqlserver.jdbc.SQLServerException: The connection to the host localhost, named instance sqlexpress failed. Error: "java.net.SocketTimeoutException: Receive timed out". Verify the server and instance names and check that no firewall is blocking UDP traffic to port 1434. For SQL Server 2005 or later, verify that the SQL Server Browser Service is running on the host.
at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDriverError(SQLServerException.java:234)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.getInstancePort(SQLServerConnection.java:6132)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.primaryPermissionCheck(SQLServerConnection.java:2609)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.login(SQLServerConnection.java:2346)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.connectInternal(SQLServerConnection.java:2213)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.connect(SQLServerConnection.java:1276)
at com.microsoft.sqlserver.jdbc.SQLServerDriver.connect(SQLServerDriver.java:861)
at java.sql/java.sql.DriverManager.getConnection(DriverManager.java:677)
at java.sql/java.sql.DriverManager.getConnection(DriverManager.java:228)
at net.codejava.sql.JavaConnect2SQL.main(JavaConnect2SQL.java:16)
The code I do have is:
import java.sql.DriverManager;
import java.sql.SQLException;
//import com.sun.jdi.connect.spi.Connection;
public class JavaConnect2SQL {
public static void main(String[] args) {
// TODO Auto-generated method stub
String url = "jdbc:sqlserver://localhost\\SQLEXPRESS;databaseName=students"; // This is where I think the error is
String user = "sa";
String password = "123";
try {
java.sql.Connection connection = DriverManager.getConnection(url, user, password);
System.out.println("Succesfully connected to Microsoft SQL Server");
}catch (SQLException e) {
System.out.println("Oops! There was an error: ");
e.printStackTrace();
}
}
}
I think the problem is in the connection URL, but I don't know the host name/instance name. If you can tell me how to get my host name/instance name, that would be appreciated. But if the problem is something else, please tell me!
Edit: I have changed the code, and I'm now getting a different. Here is the new code:
package net.codejava.sql;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class JavaConnect2SQL {
public static void main(String[] args) {
// Create a variable for the connection string.
String url = " jdbc:sqlserver://LAPTOP-5697KK36:1433;databaseName=students";
String user= "sa";
String password = "123";
try {
Connection connection = DriverManager.getConnection(url, user, password);
System.out.println("Connection succesful!");
}
// Handle any errors that may have occurred.
catch (SQLException e) {
System.out.println("Here is your error message: ");
e.printStackTrace();
}
}
}
The new error message is this:
java.sql.SQLException: No suitable driver found for jdbc:sqlserver://LAPTOP-5697KK36:1433;databaseName=students
at java.sql/java.sql.DriverManager.getConnection(DriverManager.java:702)
at java.sql/java.sql.DriverManager.getConnection(DriverManager.java:228)
at net.codejava.sql.JavaConnect2SQL.main(JavaConnect2SQL.java:40)
I think the problem is that I installed the wrong version of JDBC Driver. My JRE version is 12.0.1. What is the correct JDBC Driver to install.
You can check in SQL server configuration manager and Microsoft SQL server management studio. The state must be running in server configuration manager and you can try to connect your database in Microsoft SQL server management with your password.
if you connect successfully then there is another problem but it looks like the database is stopped.
I want to write a code to get my queries from input and run it and show me the result.I have to connect to database in my code.
I connected to postgres by intellij database extension and my queries run in the console.
but i want to do that in my code(i mean get the queries from user and run it).
is it possible to use this database connection and run queries on it?
i got connected successfully by this code too :
Connection con = DriverManager.getConnection("jdbc:postgresql://localhost:5432/db","postgres", "pass");
and i wrote some queries.
all query types(such as update , delete , insert , ...) run and the result is visible in the pgadmin but i want to have the result in my java code
Here is an example to run a query with JDBC. you can download the JDBC here https://jdbc.postgresql.org/download.html and added to your classpath
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class PostgreSqlExample {
public static void main(String[] args) {
try (Connection connection = DriverManager.getConnection("jdbc:postgresql://localhost:5432/example", "postgres", "postgres")) {
System.out.println("Java JDBC PostgreSQL Example");
System.out.println("Connected to PostgreSQL database!");
Statement statement = connection.createStatement();
System.out.println("Reading car records...");
System.out.printf("%-30.30s %-30.30s%n", "Model", "Price");
ResultSet resultSet = statement.executeQuery("SELECT * FROM public.cars");
while (resultSet.next()) {
System.out.printf("%-30.30s %-30.30s%n", resultSet.getString("model"), resultSet.getString("price"));
}
} /*catch (ClassNotFoundException e) {
System.out.println("PostgreSQL JDBC driver not found.");
e.printStackTrace();
}*/ catch (SQLException e) {
System.out.println("Connection failure.");
e.printStackTrace();
}
}
}
I have a problem connecting to SQL-server database through from my android project. I have added sqljdbc41.jar file to my /app/libs directory and I have added it to dependencies in my android studio project.
I use following code:
package com.konrad.rezerwacje1;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
public class Database_Console {
public static void openConnection(){
try {
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
String url = "jbdc:sqlserver://127.0.0.1:1433;databaseName=my_db";
Connection con = DriverManager.getConnection(url);
} catch (SQLException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
public static void main(String[] args){
openConnection();
}
}
yet i still get this error
java.sql.SQLException: No suitable driver found for jbdc:sqlserver://127.0.0.1:1433;databaseName=my_db
at java.sql.DriverManager.getConnection(DriverManager.java:689)
at java.sql.DriverManager.getConnection(DriverManager.java:270)
Instead of this :
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
String url = "jbdc:sqlserver://127.0.0.1:1433;databaseName=my_db";
You have to use this :
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
String url = "jdbc:sqlserver://127.0.0.1:1433;DatabaseName=my_db";
Connection con = DriverManager.getConnection(url, "username", "password");
Note the different classname, and the fact that prefix jbdc in the URL has been changed to jdbc.
If it is not a requirement to go with sqljdbc41.jar, then you might consider using the jtds driver for your requirement to connect to SQL Server 2014 with Android Studio. There are tons of articles that can help you start with this set of technologies.
For a primer, here are the details:
Download the JTDS driver from here
Then import this jar into your Android Studio, eg: jtds-1.2.5.jar
Use the following details in your code:
Class.forName("net.sourceforge.jtds.jdbc.Driver").newInstance();
DriverManager.getConnection("jdbc:jtds:sqlserver://127.0.0.1:1433/DATABASE;user=sa;password=p#ssw0rd");
I am using the below code to connect to MySQL
import java.sql.DriverManager;
import com.mysql.jdbc.Connection;
public class connectMysql {
public static void main(String[] args){
Connection conn = null;
try{
conn = (Connection)DriverManager.getConnection("jdbc:mysql://localhost:3306//test","root","admin");
if(conn!=null)
{
System.out.println("Connected successfully");
}
}catch(Exception e)
{
System.out.println("Not connected");
e.printStackTrace();
}
}
}
The username: root, password: admin, Hostname: localhost, Port: 3306.
I get the output as "Not connected".
[Edits] Now I Stack Trace and see that error is 'Unknown database '/test'. But I do have a schema named test. Is the schema same as the database?
Everything is same as this. But I don't seem to get connected to the DB.
Thanks for the help!!!
Why you have double slash // before your database name in your connection string
jdbc:mysql://localhost:3306//test","root","admin"
Change it to
jdbc:mysql://localhost:3306/test","root","admin"
Check all the drivers are available in the Library
Check for the port number, Username and password(case Sensitive)
if every thing is correct then
use the below code
Class.forName("com.mysql.jdbc.Driver");
// Setup the connection with the DB
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "username", "password");
statement = con.createStatement();
I need to access database with remote mysql server, but when i try it, i get an error like this :
SQLException: Access denied for user 'u121252012_iya'#'114.79.63.156' (using password: YES)
SQLState: 28000
VendorError: 1045
and this is my connection code :
package Include;
import java.sql.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
/**
*
* #author Ruslan
*/
public class Koneksi {
private static Connection koneksi;
public static void main (String []args){
Connection conn = null;
try {
conn =
DriverManager.getConnection("jdbc:mysql://my_ip_server/u121252012_coba?" +
"user=u121252012_iya&password=qwertyu");
// Do something with the Connection
} catch (SQLException ex) {
// handle any errors
System.out.println("SQLException: " + ex.getMessage());
System.out.println("SQLState: " + ex.getSQLState());
System.out.println("VendorError: " + ex.getErrorCode());
}
}
}
please, i need your assistance to solve it.
CREATE USER '<username>'#'%' IDENTIFIED BY '<pwd>';
Here % indicates user is allowed access from all IP.
Also, in addition you might need to grant permissions to the user
GRANT ALL PRIVILEGES ON *.* TO '<username>'#'%' WITH GRANT OPTION;
And also, the connection mechanism should be like.
conn = DriverManager.getConnection(<DB_URL>,<USER>,<PASS>);
So change
conn =
DriverManager.getConnection("jdbc:mysql://my_ip_server/u121252012_coba?" +
"user=u121252012_iya&password=qwertyu");
To
conn =
DriverManager.getConnection("jdbc:mysql://my_ip_server/u121252012_coba?",
"u121252012_iya","qwertyu");