JDBC connecting java to sql server database - java

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)

Related

Why is this mySQL database not connecting to Netbeans?

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 .

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/

How do I login to 'xe' connection in Oracle 11g with java code?

I want to make java code that does only this:
Login to the 'xe' connection in Oracle 11g database. That's all.
How can do it ?
EDIT:
yes, i was using JDBC, but unable to login to that connection. My url is jdbc:oracle:thin:#//localhost:1521/xe, username = sys and password = 123456 for the xe or sys connection. Then why can't i login to that connection ?
EDIT:
I am very sorry, I forgot to add that I see another error besides the 1st one, i.e.
SQLException: ORA-28009: connection as SYS should be as SYSDBA or SYSOPER
This is followed by-
Exception in thread "main" java.lang.NullPointerException
If you want to connect as SYS you have to use sys as sysdba
So in your java code try like the following code
String url = "jdbc:oracle:thin:#//localhost:1521:xe";
String username = "sys as sysdba";
String password = "123456";
Connection connection= DriverManager.getConnection(url, username, password);
Regards
Unfortunately, your answer is a bit too vague.
If you want to login to databases through Java, use JDBC.
If you're wanting to automate the logging in of an external program, you've got the wrong language :)
If you're wanting to verify the database is up, see the first point.
Overall, look to JDBC. Oracle's documentation of JDBC can be found here.
You should write the following code :
private String driver="oracle.jdbc.driver.OracleDriver";
private String dbURL="jdbc:oracle:thin:#localhost:1521:XE";
private String dbUserName="sys as sysdba";
private String dbPassword="123456";
try{
Class.forName(driver);
con=DriverManager.getConnection(dbURL,dbUserName,dbPassword);
-----//Rest of your Code//-------
}catch(Exception e){
e.printStackTrace();
}

When using JDBC with Oracle DB how to find if the connection really timed out or if credentials were wrong?

I already have the connection string and DBA username. I accept DBA password from the user.
To check if the password provided by him is correct, I try to create a connection using
String connString = "jdbc:oracle:thin:#" + connDesc;
Properties props = new Properties();
props.put("user", user);
props.put("password", pwd);
props.put("internal_logon", "sysdba");
Class.forName("oracle.jdbc.OracleDriver").newInstance();
Connection conn = DriverManager.getConnection(connString, props);
Now when it throws SQLException I need to find if that exception is because of wrong password or network timeout. Is there any way to do that?
Also, is there any better way to validate password than what I'm currently doing?
You can use the SQLException.getErrorCode() method to read the Oracle-specific error code for the error.
For an invalid username/password combination, the error code is 1017.
It's not clear to me exactly what error you mean by 'network timeout'. If the message is The Network Adapter could not establish the connection, then the error code you want appears to be 17002. (I got this error attempting to connect to my local Oracle XE instance using JDBC but with the TNS listener shut down.)
There isn't a better way of validating a username/password than attempting to create a connection to the database using that username and password.

Categories