Why is this mySQL database not connecting to Netbeans? - java

The mysql database will not connect to the netbeans code. I am using a Mac Device and have installed mySQL on my device (it is running fine on localhost:8080). However, the connection through Java is not working. I believe there may be an error in the following line "conn = Driver Manager..." since it is not executing. I am unsure of how to change the password / what the password is.
Restarting Xampp
Screenshot of connection code
Expected result: user input is sent to mySQL database
Actual result (error): seen in this screenshot

This is the general database connectivity code .
try
{
Class.forName("java.sql.DriverManager");
Connection con=(Connection) DriverManager.getConnection("jdbc:mysql://localhost:3306/DatabaseName","MysqlUsername","MysqlPassword");
Statement stmt = (Statement) con.createStatement();
String query = "YourMysqlQuery";
stmt.executeUpdate(query);
}
catch(Exception e)
{
JOptionPane.showMessageDialog(this, e.getMessage());
}
// You also have to add 'jdbc driver' in lib folder of your project.
If want us to solve the problem in your code then please provide the actual code you have done , Not the image .

Related

JDBC connecting java to sql server database

public Connection getConnection()throws Exception {
String url = "jdbc:sqlserver://localhost:1433;databaseName=PRJ311;integratedSecurity=true;";
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
return DriverManager.getConnection(url, userID, password);
}
So basically I have just installed ms sql server 2017 and I used default settings for developer. Do I need a username and password? And if it's needed, how can I get it? I have been googling the whole night so hopefully someone can help. Thanks!\n
P/s: My program is stuck and even though I use try-catch blocks, it prints out no error at all!
The connection name specified by sql server is: localhost\MSSQLSERVER01 (DESKTOP-TG6LRB4\emsnguyen)

How to connect to mysql using eclipse running apache

I am creating a dynamic web project in eclipse. I have my local apache server running and configured with appropriate resource, mysql running and configured with the appropriate port.
I downloaded the appropriate driver, included it in the lib directory - I even tried adding it as an external JAR file to no avail. On the dynamic web page the result is "error connecting to database".
I created a JSP with the following code:
<%# page import="java.sql.*"%><%# page import="java.io.*"%><%# page import="com.mysql.*"%><?xml version="1.0"?>
<tours>
<%
Connection connection = null;
Statement statement = null;
ResultSet result = null;
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
connection = DriverManager.getConnection("jbdc:mysql://localhost:3306/tours", "root", "root");
out.println("connected to database!");
}
catch(SQLException e) {
out.print("error connecting to database");
}
%>
</tours>
Please advise...
Thanks in advance.
Put print after Class.forName() check, if that statement is not printing then problem is driver if that line is printing then problem is mysql database name problem or credential problem
If problem in drive then download from here :
http://www.java2s.com/Code/Jar/c/Downloadcommysqljdbc515jar.htm
Problem is u put jbdc instead of jdbc
Code like this
<%
Connection connection = null;
Statement statement = null;
ResultSet result = null;
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
out.println("Driver is available !");
connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/tours",
"root", "root");
out.println("connected to database!");
}
catch(SQLException e) {
out.print("error connecting to database");
}
%>
There are couple of things to take care of here.
1. Make sure that mysql is listening on port 3306.
2. Cross check the database name.
Lastly get the driver jar file in deployment assembly.
If the problem still persists, please print the stacktrace and post it here.
Please check weather your database is started or not i know it should be in comment but i cannot keep it in comment because i don't have sufficient reputations. please try it if it helps you it is fine.
How to start database is shown below:
Click Windows button+r then enter Services.msc in search bar then select Mysql and click start option
Printing the stack trace might help it could be because of authorization issue. If the error is related to authorization issue, you may have to change the server configurations.

How to connect to a database using a distributed API?

I'm having trouble with my distributed application in java netbeans the program is running perfectly on the original machine where i created it. but when i build it and make a distribute folder and follow the instructions and try to run it, I got an error that localhost on port 1527 has been refused.
here is my code on my do connect function
public void DoConnect()
{
String host = "jdbc:derby://localhost:1527/KempDB";
String uName = "main";
String uPass = "admin";
try
{
con = DriverManager.getConnection(host, uName, uPass);
stmt = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
String sql = "select cv.checkvouchernumber, c.checknumber, paytoorder, bankcode, dateissued, amount from checkvoucher cv, checks c where cv.checkvouchernumber = c.checkvouchernumber and cv.checknumber = c.checknumber";
rs = stmt.executeQuery(sql);.....
..........
}
catch(SQLException err){
.......
}
so this is the code I used to connect with the database server, the database server I used is built-in with java. Its a apache derby...
like I said in the original machine where I created the program runs ok without errors but when I distribute the program to another machine there's an error refusing the connection.
How can I connect to the local machine where my database is? maybe you can help me on this.
Are the database Servers running on these machines?
Are u starting the database server programmatically?
If you try to connect a database Server with:
jdbc:derby://localhost:1527/KempDB
This Server needs to be up and running.
For your case you should use an embedded Database.
For the case your Database is already an embedded Database, then you can try using this URL:
jdbc:derby:KempDB
instead of:
jdbc:derby://localhost:1527/KempDB
Hav a look on this one

Connect to mysql remote database using JDBC?

I'm a java beginner and I'm working on a simple application which connects to a remote mysql database using JDBC. I've tested it locally and it works just fine, however I cannot get it to work on for my remote server.
I don't think its of much use but heres the code:
Connection connection = null;
String dburl = "jdbc:mysql://314159265:3306/Db_Name";
String userName = "user";
String passWord = "password";
try {
Class.forName("com.mysql.jdbc.Driver");
connection = DriverManager.getConnection(dburl, userName, passWord);
Statement st = connection.createStatement();
String query = "INSERT INTO Example (`TestColumn`) VALUES('hello')";
int rsI = st.executeUpdate(query);
System.out.println("Hi");
}catch (Exception e) {
System.out.println(e);
} finally {
if (connection != null) {
try {
connection.close();
System.out.println("Database connection terminated");
} catch (Exception e) { /* ignore close errors */ }
}
}
When I run this, I get the following message:
com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure
The last packet sent successfully to the server was 0 milliseconds ago.The driver has not received any packets from the server.
I'm pretty sure it must be some kind of server configuration issue.
Notes:
Username, password, IP, database name, etc. are just examples.
This could be a firewall problem, or a configuration problem. But I don't think it is a coding problem at all - you need to start troubleshooting the connection.
Trouble shoot by attempting to use third party client apps to connect to mysql. This will indicate whether it is configured for external access. Although it doesn't ensure that JDBC is visible from the outside, it does rule out some potential firewall problems.
Follow this guide to help you mess with your configurations
Remote MYSQL Database Access
If you are still stuck, it could be a coding problem so check out this page:
How to connent to a remote mysql database with java?
P.S. I am assuming you are using unix as the operating system.
I guess 314159265 could be replaced by some address....
like jdbc:mysql://localhost:3306/
or jdbc:mysql://127.0.0.1:3306/

problems in connecting java with ms access

hye all..
i am currently doing my final year project in java.i want to connect a ms access database using java.But sadly i'm using windows 7 starter that cannot support odbc.do you have any idea i can solve this problem?thank for all your coming idea and help =)
Simply try to have another OS get installed or try to have some of the drivers that support JDBC and you will surely able to work in connecting dtabase with java.
You could try JDBC drivers like these:
http://www.csv-jdbc.com/stels_mdb_jdbc.htm
http://www.hxtt.com/access.html
Both are commercial products.
A free option would be to migrate the database to SQL Server Express and then use the SQL Server JDBC drivers to connect to that.
I used the sun.jdbc.odbc.JdbcOdbcDriver to connect to a MS Access database. Have that in the same directory as the class file and it should work. Although it should come already installed in the Java SDK.
This is a sample of a practice program I made a while ago.
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
System.out.println("Driver loaded");
// Establish a connection
Connection connection = DriverManager.getConnection
("jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ=(MS ACCESS DATABASE DIRECTORY)");
System.out.println("Database connected");
// Create a statement
Statement statement = connection.createStatement();
// Execute a statement
ResultSet resultSet = statement.executeQuery
("select f_name, l_name from Test where f_name = 'Luke'"); // For example
// Iterate through the result and print the results
while (resultSet.next())
System.out.println(resultSet.getString(1) + "\t" + resultSet.getString(2) );

Categories