I am trying to connect to a local MariaDB Database via eclipse and tomcat. When I run the Program i get this message:
java.sql.SQLNonTransientConnectionException: Could not connect to localhost:8080 : unexpected end of stream, read 0 bytes from 4 (socket was closed by server)
at org.mariadb.jdbc.internal.util.exceptions.ExceptionMapper.get(ExceptionMapper.java:240)
at org.mariadb.jdbc.internal.util.exceptions.ExceptionMapper.getException(ExceptionMapper.java:171)
at org.mariadb.jdbc.internal.protocol.AbstractConnectProtocol.connectWithoutProxy(AbstractConnectProtocol.java:1132)
at org.mariadb.jdbc.internal.util.Utils.retrieveProxy(Utils.java:561)
at org.mariadb.jdbc.MariaDbConnection.newConnection(MariaDbConnection.java:175)
at org.mariadb.jdbc.Driver.connect(Driver.java:92)
This is the code i am executing:
public void verbinde() {
Connection conn = null;
Statement stmt = null;
try {
//STEP 2: Register JDBC driver
Class.forName("org.mariadb.jdbc.Driver");
//STEP 3: Open a connection
System.out.println("Connecting to a selected database...");
conn = DriverManager.getConnection(
"jdbc:mariadb://localhost:8080/test?user=root&password=root");
System.out.println("Connected database successfully...");
//STEP 4: Execute a query
System.out.println("Creating table in given database...");
stmt = conn.createStatement();
String sql = "CREATE TABLE REGISTRATION "
+ "(id INTEGER not NULL, "
+ " first VARCHAR(255), "
+ " last VARCHAR(255), "
+ " age INTEGER, "
+ " PRIMARY KEY ( id ))";
stmt.executeUpdate(sql);
System.out.println("Created table in given database...");
} catch (SQLException se) {
//Handle errors for JDBC
se.printStackTrace();
} catch (Exception e) {
//Handle errors for Class.forName
e.printStackTrace();
} finally {
//finally block used to close resources
try {
if (stmt != null) {
conn.close();
}
} catch (SQLException se) {
}// do nothing
try {
if (conn != null) {
conn.close();
}
} catch (SQLException se) {
se.printStackTrace();
}//end finally try
}//end try
System.out.println("Goodbye!");
}//end main
//end JDBCExample
the default port of MariaDB is 3306 not 8080
jdbc:mariadb://localhost:3306/test?user=root&password=root
8080 is the port where your tomcat is running
BTW: use try-with-resources instead of the try-catch-finally block
Related
I need to convert result set into csv for any database (not just postgres)
Empty csv file is being created when I use opencsv.
Here's the code of doGet method in the servlet:
final String JDBC_DRIVER = "org.postgresql.Driver";
final String DB_URL = "jdbc:postgresql://localhost:5432/postgres";
// Database credentials
final String USER = "postgres";
final String PASS = "12345";
response.setContentType("text/html");
PrintWriter out = response.getWriter();
String title = "Database Result";
String docType =
"<!doctype html public \"-//w3c//dtd html 4.0 " +
"transitional//en\">\n";
out.println(docType +
"<html>\n" +
"<head><title>" + title + "</title></head>\n" +
"<body bgcolor=\"#f0f0f0\">\n" +
"<h1 align=\"center\">" + title + "</h1>\n");
PreparedStatement ps = null;
Connection conn = null;
try {
// Register JDBC driver
Class.forName("org.postgresql.Driver");
// Open a connection
conn = DriverManager.getConnection(DB_URL, USER, PASS);
// Execute SQL query
//stmt = conn.createStatement();
String sql = "SELECT * FROM users";
ps = conn.prepareStatement(sql,
ResultSet.TYPE_SCROLL_SENSITIVE,
ResultSet.CONCUR_UPDATABLE);
ResultSet rs = ps.executeQuery();
/*if(rs.next()){
System.out.println("Name = "+rs.getString("first_name"));
}*/ //prints name so rs is not empty
//rs.first();
CSVWriter writer = new CSVWriter(new FileWriter("Test.csv"));
//even tried with seperator '\t' or ','
writer.writeAll(rs, true);
writer.close();
out.println("</body></html>");
// Clean-up environment
rs.close();
ps.close();
conn.close();
} catch (SQLException se) {
//Handle errors for JDBC
se.printStackTrace();
} catch (Exception e) {
//Handle errors for Class.forName
e.printStackTrace();
} finally {
//finally block used to close resources
try {
if (ps != null)
ps.close();
} catch (SQLException se2) {
}// nothing we can do
try {
if (conn != null)
conn.close();
} catch (SQLException se) {
se.printStackTrace();
}//end finally try
} //end try
Not sure what's the error. Tried different way but csv is always empty.
Even tried writer.flush(), rs.beforeFirst(), rs.first() nothing works.
Is your problem that you do not see data in the html - if that is the case then instead of creating a new FileWriter in the CSVWriter just pass in you out variable.
Or is it that you checked the Test.csv and file and nothing is there? if so then first check to see if there is actually data in the result set by adding the following after executeQuery:
rs.last();
long numberOfRecords = rs.getRow();
rs.beforeFirst();
System.out.println("Number of Users in table is: " + numberOfRecords);
I have the following code, which does not get to the .setText("Successful") statement, indicating an issue with the drivermanager.getConnection statemenet (I think). It finds the database driver that I'm using from net.sourceforge. But there is no exception error message thrown, nothing happens:
String connectionurl = "jdbc:jtds:sqlserver://41.185.13.201; databaseName=Courses; user=*;Password=*;Persist Security Info=True;";
try {
Class.forName("net.sourceforge.jtds.jdbc.Driver").newInstance();
Connection con = DriverManager.getConnection(connectionurl);
textview7.setText("Successful");
// Create and execute an SQL statement that returns some data.
String SQL = "INSERT INTO Courses (CourseCode) VALUES ('INFO3002')";
Statement stmt = con.createStatement();
stmt.executeUpdate(SQL);
con.close();
}
catch (ClassNotFoundException e) {
textview7.setText("Could not find the database driver " + e.getMessage());
} catch (SQLException e) {
textview7.setText("Could not connect to the database " + e.getMessage());
}
catch (Exception e) {
//textview7.setText(e.getMessage());
}
You should access your data through web services (JAX-WS or JAX-RS). It is the best architecture you can use. As said above, you should simply develop a middleware.
I have 1 basic program and 1 app, My basic program with this works ( DB_URL, USER, PASS, JDBC_DRIVER all correct and functional) and I am able to get information from my MySQL DB. The code consist of this:
try {
// STEP 2: Register JDBC driver
Class.forName(JDBC_DRIVER);
// STEP 3: Open a connection
conn = DriverManager.getConnection(DB_URL, USER, PASS);
// STEP 4: Execute a query
System.out.println("Creating statement...");
stmt = conn.createStatement();
String sql;
sql = "SELECT distinct tags FROM items";
ResultSet rs = stmt.executeQuery(sql);
// STEP 5: Extract data from result set
while (rs.next()) {
// Retrieve by column name
String itemRoles = rs.getString("tags");
//Add it to the ArrayList.
itemRolesList.add(itemRoles);
// Display values
System.out.print("TAGS: " + itemRoles + "\n");
}
// STEP 6: Clean-up environment
rs.close();
stmt.close();
conn.close();
} catch (SQLException se) {
// Handle errors for JDBC
se.printStackTrace();
} catch (Exception e) {
// Handle errors for Class.forName
e.printStackTrace();
} finally {
// finally block used to close resources
try {
if (stmt != null)
stmt.close();
} catch (SQLException se2) {
}// nothing we can do
try {
if (conn != null)
conn.close();
} catch (SQLException se) {
se.printStackTrace();
}// end finally try
}// end try
But when I try to apply this same code to my app (inside a Fragment in my OnCreateView()) I get this:
"java.lang.ClassNotFoundException: com.mysql.jdbc.Driver"
and it is at this line of code:
Class.forName(JDBC_DRIVER);
I added the "mysql-connector-java-5.1.23-bin.jar" and it is in my Reference Library in both my program and app. Does anyone know why inside my app it gives me this error?
Make sure that you have driver jar in your classpath.
Please verify that you have jar file inside lib folder is not the empty jar file.sometime during wrong confuguration it shows a empty jar file.
please go to project properties-->buildpath-->libraries
Check out the jar file you have added is not blank.
So I have tried using the stock Play! 2.2 configuration for the MySql database connection. Unfortunately the guides out there are less than helpful when using the stock database (h2) alongside a MySql. SO, I coded a separate model to handle the MySql connection. It works intermittently, and I'm trying to figure out why it doesn't work all of the time.
this is the "connect" function
String sourceSchema = "db";
String databaseHost = "host";
String databaseURLSource = "jdbc:mysql://" + databaseHost + "/" + sourceSchema;
String databaseUserIDSource = "userid";
String databasePWDSource = "password";
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
conn = DriverManager.getConnection(databaseURLSource,
databaseUserIDSource, databasePWDSource);
return true;
} catch (InstantiationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
Logger.error("SQLException: " + e.getMessage());
}
All of my credentials are correct (here obviously they are changed) Next, in my lib folder, I have the
mysql-connector-java-5.1.21-bin.jar
in place.
Next, in my Build.scala, I have this under appDependencies:
"mysql" % "mysql-connector-java" % "5.1.21"
when I try to validate the connection, using:
public boolean isConnected() {
return conn != null;
}
The connection fails (intermittantly) and then gives me:
SQLException: Before start of result set
and sometimes:
SQLException: No Suitable driver found for mysql ...
This is how my query is executed:
String qs = String.format("SELECT * FROM community_hub.alert_journal LIMIT("+ from +","+ to +")");
String qscount = String.format("SELECT COUNT(*) AS count FROM community_hub.alert_journal");
try {
if (isConnected()) {
Statement stmt = conn.createStatement();
//obtain count of rows
ResultSet rs1 = stmt.executeQuery(qscount);
//returns the number of pages to draw on index
int numPages = returnPages(rs1.getInt("count"),rpp);
NumPages(numPages);
ResultSet rs = stmt.executeQuery(qs);
while (rs.next())
{
AlertEntry ae = new AlertEntry(
rs.getTimestamp("date"),
rs.getString("service_url"),
rs.getString("type"),
rs.getString("offering_id"),
rs.getString("observed_property"),
rs.getString("detail")
);
list.add(ae);
}
rs.close();
disconnect();
} else {
System.err.println("Connection was null");
}
}
catch (Exception e)
{
e.printStackTrace();
}
Help?
Thanks!
does the mysql error tell you anything?
the first error "SQLException: Before start of result set" looks like its incomplete. Maybe the error log contains the full message or you can
the second one "SQLException: No Suitable driver found for mysql" clearly indicates a classpath issue.
usually connection pools like c3p0 or BoneCP recommed to use a validation query to determine if a connection is valid (something like "select 1" for mysql). That may help to make sure the connection is ok and not rely on the driver?
I have a cvs file which schema is, every field is surrounded with ", and seperated by , and every tuple is a newline with \n
So in my Java file, I wrote
String path = "o.csv";
String esquel = " LOAD DATA LOCAL INFILE " + path +
" INTO TABLE recommendations " +
" FIELDS TERMINATED BY \',\' ENCLOSED BY \'\"'" +
" LINES TERMINATED BY \'\\n\'";
And I execute the statement with the following statement
statement.executeUpdate(esquel);
But it throws an SQLException which says:
com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an
error in your SQL syntax; check the manual that corresponds to your
MySQL server version for the right syntax to use near 'o.csv INTO
TABLE recommendations FIELDS TERMINATED BY ',' ENCLOSED BY '"' LINES'
at line 1
What is my error ?
I would be appreciate if you can help me.
Thanks
Here is working code which I tested:
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class LoadTRPLog2MySql {
public static void main(String[] args) {
Class driver_class = null;
try {
driver_class = Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
return;
}
System.out.println("found driver" + driver_class);
Connection connection = null;
try {
connection = DriverManager.getConnection("jdbc:mysql://mysqlserver.com:3306/dbname", "myid","pwd");
} catch (SQLException e) {
e.printStackTrace();
}
try {
System.out.println("Established connection to " + connection.getMetaData().getURL());
} catch (SQLException e1) {
e1.printStackTrace();
}
Statement statement = null;
try {
statement = connection.createStatement();
Statement statement1 = connection.createStatement();
//windows
//statement1.executeUpdate( "LOAD DATA LOCAL INFILE 'C:\\Users\\senthil_sivasamy\\Documents\\Projects\\messageprocessing\\log.txt' INTO TABLE trpwatchlog_tb FIELDS TERMINATED BY ' ' LINES TERMINATED BY '\\n'");
//linux ( " LOAD DATA LOCAL INFILE '/home/username/logname.log' INTO TABLE logname.log FIELDS TERMINATED BY ' ' LINES TERMINATED BY '\\n'");
statement.executeUpdate( "LOAD DATA LOCAL INFILE '/home/username/avail30trplog' INTO TABLE logname.log FIELDS TERMINATED BY ' ' LINES TERMINATED BY '\\n'");
statement1.execute("select * from dbname.tablelog_tb");
ResultSet rs = statement1.getResultSet();
System.out.println("Row hostname and timestamp");
while(rs.next()) {
System.out.println("Row hostname and timestamp");
System.out.println(rs.getRow());
System.out.println(""+rs.getString("hostname"));
System.out.println(""+rs.getString("timestamp"));
}
rs.close();
} catch(SQLException e) {
} finally {
if (statement != null) {
try {
statement.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
if (connection != null) {
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
Oh I got it ! I didn't surround my path file with '.
New sql statement should be:
String esquel = " LOAD DATA LOCAL INFILE '" + path +
"' INTO TABLE recommendations " +
" FIELDS TERMINATED BY \',\' ENCLOSED BY \'\"'" +
" LINES TERMINATED BY \'\\n\'";