Unable to access H2 Database in LAN - java

Please go through below code that I wrote in Java Swing to connect H2 Database in LAN, I did Google but not get proper solution.
try {
//192.168.0.200 is Partner IP Address
Class.forName("org.h2.Driver");
Connection connection = DriverManager.getConnection("jdbc:h2:tcp://192.168.0.200/~/testingDB", "sa", "");
System.out.println("Connected" + connection);
} catch (Exception e) {
System.out.println("Here" + e.toString());
}
I am trying to connect H2 Database that are installed on another computer using IPV4 address but I am getting below error.
Hereorg.h2.jdbc.JdbcSQLException: Remote connections to this server are not allowed, see -tcpAllowOthers [90117-184]
at org.h2.message.DbException.getJdbcSQLException(DbException.java:345)
at org.h2.message.DbException.get(DbException.java:179)
at org.h2.message.DbException.get(DbException.java:155)
at org.h2.message.DbException.get(DbException.java:144)
at org.h2.server.TcpServerThread.run(TcpServerThread.java:83)
at java.lang.Thread.run(Unknown Source)
I given below command in partner as well as my computer but still I am getting the same error.
http://www.windows-commandline.com/enable-remote-desktop-command-line/
Please help me to find the solution.

You need to start the H2 server with the -tcpAllowOthers option. This option is not enabled by default for security reasons.

Related

Cannot connect to SQL Server from Java

I have a problem when I try to connect to SQL Server with Java. I have the next error:
The connection to the host DESKTOP-C0SCI39, named instance 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.
My firewall is off and I don't know what is wrong(mssql-jdbc-8.4.1.jre14 is included). This is my Java code:
String url = "jdbc:sqlserver://DESKTOP-C0SCI39\\;databaseName=students";
try {
Connection connection = DriverManager.getConnection(url);
System.out.println("Connected to MS SQL");
} catch (SQLException throwables) {
System.out.println("Error!");
throwables.printStackTrace();
}
Try this solution, I have ran into similar problems in the past and have found that these help.
Check that the JDBC Driver is imported in the project. (Preferred - 5.1.48)
Check that your SQL Server is up and running (Check if SQL Service is running)
Check if you entered your connection string properly
eg: private String URL = "jdbc:mysql://localhost:3306/dbName"; //database url
Try adding a useSSL=false to the end of your connection string dbName?useSSL=false
Open your server with a server management software such as SQL Server Management Studio to see if your SQL Server is working as it should
Your database is not running where you think it is running. Verify the port and location of the server and try again.

Java SQL Connection Not Functioning

When I make a connection to my MYSQL database (hosted on phpmyadmin on a domain with hosting, not localhost), I receive an error that I'm denied access to the server entirely. What is going on? I'm not extremely familiar on how to use this libraries methods and am confused.
Connection connection;
try
{
connection =
DriverManager.getConnection("jdbc:mysql://WEBSITENAME.com/DATABASENAME0",
"DATABASEUSERNAME", "DATABASEPASSWORD");
Statement sql = connection.createStatement();
ResultSet myRs = sql.executeQuery("SELECT * FROM table");
while(myRs.next())
{
System.out.println(myRs.getString("column"));
}
} catch (SQLException e)
{
e.printStackTrace();
}
The error provided is as follows:
java.sql.SQLException: Access denied for user 'user'#'IP ADDRESS IS LISTED HERE' (using password: YES)
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:129)
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:97)
at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:122)
at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:835)
at com.mysql.cj.jdbc.ConnectionImpl.(ConnectionImpl.java:455)
at com.mysql.cj.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:240)
at com.mysql.cj.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:199)
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at jDgmj8pWUnXVoHZk04z9.sqlConnect.updateCommand(sqlConnect.java:12)
at jDgmj8pWUnXVoHZk04z9.Driver.main(Driver.java:10)
I have two ideas for this:
Normally you MySQL denies connections from other hosts. You have to configure that in the configuration file (see here). If you can not access the configuration file you will have to ask your admin.
Additionally you should check if the port 3306 (MySQL default port for TCP connections) is even opened. If the firewall blocks that port you will not be able to connect. In Windows you could check it with telnet (if enabled):
.
telnet WEBSITENAME.com 3306
No error message (blank cmd) means connection is established.
Maybe one of the problem is the Server, check that the user is allowed to connect from outside the localhost, or check if you user is allowed to connect like this: user#IPAddress
Common problem for hosted services, like hosted DBs/DBaaS nowadays. Many hosting providers default to DENY ALL and expect that no DB connections are allowed inbound from anonymous Internet IPs, aka. anything outside (a) their platform or (b) outside their subnet(s).
If the other recommended tests don't work (like telnet), ask your DB provider if they maintain a whitelist and how they provide exception. This is usually done via a self-service ticketing system for many hosting providers.
Or can you co-host your Java app on that same hosting provider? like via a basic VM/instance on the same subnet as the DB instance?

Unable to connect Java application to SQL Server database

I am trying to write a Java desktop app that can connect to my database made with Microsoft SQL Server Manager to allow me to view and update it. But, I am having trouble getting the connection to work. I've read through a bunch of tutorials and threads here on Stack Exchange of similar problems, and I'm not sure what I'm doing wrong.
The server is called "SQLEXPRESS" using Windows authentication. I downloaded the JDBC driver found here: https://www.microsoft.com/en-us/download/details.aspx?displaylang=en&id=11774 installed it in NetBeans by going to "Services-Databases(right click)-New Connection-Add", but I also added it as a library in my project.
When I try this code, I get the exception that the TCP/IP connection failed either because the server isn't running or port 1433 is locked:
try{
String
URL="jdbc:sqlserver://sqlexpress:1433;DatabaseName=GreenhouseManagement";
Connection conn = DriverManager.getConnection(URL,"","");
System.out.println("connected");
}catch(Exception e){
System.out.println("Oops\n"+e);
}
What do I need to change to fix this?
You might need to reconfigure your connection string into this format.
jdbc:microsoft:sqlserver://HOST:1433;DatabaseName=DATABASE
HOST in this case is most likely to be "localhost" since you are connecting on a local machine.
DATABASE will be the name of your database
Reference: http://alvinalexander.com/java/jdbc-connection-string-mysql-postgresql-sqlserver

Can't Connect OpenShift MySql from tomcat project

I'm working on an openshift tomcat project by using mysql as backend I am trying to connect database and it is not connecting and shows error message "Communications link failure Last packet sent to the server was 0 ms ago" I have connected database using this code
try{
Class.forName("com.mysql.jdbc.Driver");
}catch(ClassNotFoundException ex){
//return(ex.getMessage());
}
con = DriverManager.getConnection(MYSQL_DATABASE_HOST,MYSQL_USERNAME,MYSQL_PASSWORD);
} catch (Exception ex) {
return(ex.getMessage());
}
return "success";
I've provided the database url by hardcoding the database IP and port,when I print System.getenv("OPENSHIFT_MYSQL_DB_HOST"); I'm getting null value. please tell me anyone the error
the problem you are facing occurs when your java code is unable to connect to MySQL server. There is one more way, check if your MySQL is up and running. You can get your MySQL server's ip by
rhc port-forward -a <mysql_gear_name>
You can then explicitly give that IP in DriverManager.getConnection
From the command line, run rhc port-forward to access databases or internal ports directly.
Eg :
rhc port-forward -a <name of the gear including mysql db>

Connect Java to MS SQL Server Database using Windows Authentication

I have found some similar questions but with no helpful answers.
I need to manipulate an MS SQL Server database named PDPJ_Student from my java application and i cannot connect to it. I get the following error:
[Microsoft][SQLServer 2000 Driver for JDBC][SQLServer]Login failed for user 'LAPTOP-TITI/Titi'. The user is not associated with a trusted SQL Server connection.
and my source looks like this:
Connection con = null;
try{
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver").newInstance();
con = DriverManager.getConnection("jdbc:microsoft:sqlserver://LAPTOP-TITI;DatabaseName=PDPJ_Student", "LAPTOP-TITI/Titi", "");
}
catch(Exception e){
System.out.println("Error at connection");
e.printStackTrace();
}
When i start MS SQL Server, at authenticatin, the SERVER NAME says: LAPTOP-TITI; and the Authentication is set to Windows Authentication. The user name field says LAPTOP-TITI/Titi but it is disabled, as well as the Password field, which is empty.
I have also tried:
con = DriverManager.getConnection("jdbc:microsoft:sqlserver://LAPTOP-TITI;DatabaseName=PDPJ_Student", integratedSecurity=true);
but still nothing ..
What am I doing wrong ? Can you give me some indications please?
p.s I am not allowed to change my Authentication type, and even if I try changing to SQL Server Authentication, it doesn`t let me create any new users
Your question doesn't mention whether your client machine is running Windows or non-windows. This topic on MSDN suggests that if your client is using Linux, you'll need to purchase the DataDirect driver to get Kerberos authentication working. If you're on Windows, it should be possible with Microsoft's JDBC driver.

Categories

Resources