Java Database connection error with mysql - java

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");

Related

Howo can I connect to database with Java

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();

ODBC Connection Error using Stored Procedure in Netbeans

I am making an inventory management program. The Problem is I am using multiple SQL queries with stored procedure. Which is something like this:
try
{
CallableStatement c= m.XC.prepareCall("{call addCategory_Combobox}");
ResultSet rs = c.executeQuery();
while(rs.next())
{
String name = rs.getString("category");
jComboBox2.addItem(name);
}
rs.close();
c.close();
}
catch(Exception ex)
{
System.out.println(ex.toString());
}
Here is connection I have made so far:
public class MyConnection
{
Connection XC;Statement ST;
public MyConnection()
{
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
XC=DriverManager.getConnection("jdbc:odbc:signupcon", "sa", "123");
ST=XC.createStatement();
}
catch(Exception ex)
{
System.out.println(ex);
}
}
}
The problem is that when I run the code for the 1st time, it executes successfully. But when I run the same code for the second time. it get the following Error:java.sql.SQLException: [Microsoft][ODBC SQL Server Driver]Connection is busy with results for another hstmt
and here is the SQL Query Code:
create proc addCategory_Combobox
as
begin
Declare #Status varchar(max)
select category from Category
SELECT #Status as result
end
go

connect to database using JDBC in java

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)))

How to access to NexusDB from Java

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?

MYSQL select gets deleted values (JAVA)

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 ?

Categories