I want to access to NexusDB V3 with Java.
So I have a Java project with many files that connects to the database. Can anyone tell me if it is possible to use a Java class file for connecting to the database.
I've tried JDBC connector and the ODBC connector but nothing is working.
I have read about a bridge between them but I don't know how to make it so please help.
public class dbConnect {
public static void connect(){
Connection conn;
Statement stmt;
ResultSet rs;
String sql;
conn = null;
String url = "jdbc:mysql://localhost:3306/db_oopproject";
try{
Class.forName(driver).newInstance();
conn = DriverManager.getConnection(url,"user","12345");
stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);
sql = "Select * from user_account";
rs = stmt.executeQuery(sql);
}
catch (Exception e){
System.out.print(e.getMessage());
}
}
}
first off, your url contains "mysql", are you sure that will work for the jdbc bridge to connect to NexusDB?
Related
I'm new to Java and I want to connect with MySQL local server, but it's throwing an exception error. How can I fix the code?
Connection information:
Lport:3306
database name= siba_1
table name= test_1
Code:
package java_recap;
import java.sql.*;
public class Java_recap {
public static void main(String[] args) {
try{
Class.forName("com.mysql.jdbc.Driver");
Connection
con=DriverManager.getConnection("jdbc:mqsql://localhost:3306/siba_1","root","");
Statement stn=con.createStatement();
ResultSet rs=stn.executeQuery("select * from test_1");
while(rs.next()){
System.out.println(rs.getString(1)+" "+rs.getString(2));
}
}
catch(Exception e){
System.out.println("e");
}
}
}
DriverManager is a old way of doing things. if you still want to use DriverManger then:
Need mysql dependency add jar on classpath or use maven like below :
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.19</version>
</dependency>
Now Let's see how we can connect to our database and execute a simple select-all through a try-with-multiple-resources:
String sql = "select * from test_1";
String connectionUrl = "jdbc:mysql://localhost:3306/siba_1?
serverTimezone=UTC";
try (Connection conn = DriverManager.getConnection(connectionUrl,
"root", "");
PreparedStatement ps =
conn.prepareStatement(sql);
ResultSet rs = ps.executeQuery()) {
while (rs.next()) {
System.out.println(rs.getString(1)+" "+rs.getString(2));
// do something with the extracted data...
}
} catch (SQLException e) {
// handle the exception
}
Other way : The better way is to get a DataSource either by looking one up that your app server container already configured for you:
Context context = new InitialContext();
DataSource dataSource = (DataSource) context.lookup("java:comp/env/jdbc/siba_1");
or instantiating and configuring one from your database driver directly:
MysqlDataSource dataSource = new MysqlDataSource();
dataSource.setUser("root");
dataSource.setPassword("");
dataSource.setServerName("localhost");
and then get connections from it, same as above:
Connection conn = dataSource.getConnection();
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("select * from test_1");
...
rs.close();
stmt.close();
conn.close();
I'm trying to connect to an Oracle database using JDBC Driver and I'm handle with an error: "java.sql.SQLException: Invalid Oracle URL specified".
My code is the following:
import java.sql.*;
public class L9
{
public static void main(String args[])
{
try
{
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection con = DriverManager.getConnection("jdbc:oracle:thin;#localhost:1521:xe","user","password");
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("select * from table");
while(rs.next())
System.out.println(rs.getInt(1) + " "+rs.getString(2)+ " "+rs.getString(3));
con.close();
}
catch(Exception e)
{
System.out.println(e);
}
}
}
Anyone knows what is the problem?
It should be
jdbc:oracle:thin:#localhost:1521:xe
instead of
jdbc:oracle:thin;#localhost:1521:xe
(note the : after the "thin")
It is better to use a long format connection URL where you have the ability to pass connection descriptors. Easy Connection URL (jdbc:oracle:thin:#//localhost:1521/myorcldbservicename) ) does establish the connection but does not provide any High Availability capabilities.
jdbc:oracle:thin:#(DESCRIPTION=(ADDRESS=(HOST=myhost)(PORT=1521)(PROTOCOL=tcp))(CONNECT_DATA=(SERVICE_NAME=myorcldbservicename)))
I don't know why i am getting this error infact i write code write and also add java connector in my project.
Please any one resolve my issue
import java.sql.*;
public class jbdcdemo {
public static void main(String[] args) {
try{
//1. Get Connection to DB
Connection myConn = DriverManager.getConnection("jbdc:msql://localhost:3306/world","root","1234");
//2. Create a statement
Statement myStmt = myConn.createStatement();
//3. Execute sql query
ResultSet myRs = myStmt.executeQuery("SELECT * FROM world.city;");
//4. Process the results
while(myRs.next()){
System.out.println(myRs.getString("Name")+", "+myRs.getString("District"));
}
}
catch(Exception exc){
exc.printStackTrace();
}
}
}
Try fixing the connection string:
Connection myConn = DriverManager.getConnection("jdbc:mysql://localhost:3306/world","root","1234")
Looks like you have semicolon for the sql statement,
Below is the correct one.
ResultSet myRs = myStmt.executeQuery("SELECT * FROM world.city");
I have a mysql database on my ubuntu server. When i delete rows with MYSQL workbench on this database and than try to select these rows with my Java program, i still get the deleted rows. (I have committed the command with MYSQL workbench)
In my java program I access the database over a Java Restful webservice which also runs on my server.
my webservice looks like:
#Path("/getAll")
#GET
#Produces({MediaType.APPLICATION_XML})
public List<PV> getAllPV() {
List<PV> ret =null;
try {
ret=Database.getInstance().getAllPV();
} catch (Exception e) {
e.printStackTrace();
}
return ret;
}
My Database:
public List<PV> getAllPV() throws Exception{
Connection conn = null;
PreparedStatement ps=null;
Statement s = null;
conn = getDBConnection();
conn.setAutoCommit(false);
List<PV> ret = new ArrayList<PV>();
s = conn.createStatement();
ResultSet rs = s.executeQuery("select * from PV");
...
conn.close();
}
Has anyone an idea ?
Using the mysql-connector-java.5.1.26-bin.jar to connect my android app to a remote mysql host. Problem is, anything above minSdkVersion 9 casuses the "Communications link failure". If I have it set to 9, it connects fine and gets database information. Here's my connection code:
try {
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection(url, user, pass);
Statement st = con.createStatement();
ResultSet rs = st.executeQuery("select * from users");
ResultSetMetaData rsmd = rs.getMetaData();
while(rs.next()) {
....
}
}
catch(Exception e) {
e.printStackTrace();
}
Any ideas as to why this is happening?
Turns out the problem was that I was trying to do a network connection in the main activity. For those that might have the same problem, look into using asynctask to complete the connection.
http://developer.android.com/reference/android/os/AsyncTask.html
Here is my Development Environment:
OS: Ubuntu14.04
IDE: Eclipse Luna
DB: MySQL5.1.73
Firstly,I put the code below in OnCreate method:
Class.forName("com.mysql.jdbc.Driver").newInstance();
Connection conn = DriverManager.getConnection(url, username,
password);
Statement stmt = conn.createStatement();
String sql = "select distinct Action from attributes";
ResultSet rs = stmt.executeQuery(sql);
The connection code is OK, But It will occur:
communication link failure.The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
So it must be wrong in something else. After trying various method, and solve it using android AsyncTask. Just put the code relative to operating mysql in Class that extends AsyncTask.
Below is my code:
private class Connect extends AsyncTask<String, Void, Void> {
#Override
protected Void doInBackground(String... urls) {
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
Connection conn = DriverManager.getConnection(url, username,
password);
Statement stmt = conn.createStatement();
String sql = "select distinct Action from attributes";
ResultSet rs = stmt.executeQuery(sql);
List<String> actions = new ArrayList<String>();
while (rs.next()) {
actions.add(rs.getString("Action"));
}
} catch (Exception e) {
Log.d("MySQLConnection", e.getMessage());
}
return null;
}
}