MySQL Connection

Submitted by:David Villa

Date added:14 February, 2012

Category:MySQL

MySQL code to connect to db. Don't forget to change the parameters.

Tags: mysql connection

Code Snippet:

private Connection getConn() {

Connection conn = null;
String url = "jdbc:mysql://192.168.12.81:3306/";
String db = "hostdb";
String driver = "com.mysql.jdbc.Driver";
String user = "";
String pass = "";

try {
Class.forName(driver).newInstance();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
try {

conn = DriverManager.getConnection(url+db, user, pass);
} catch (SQLException e) {
System.err.println("Mysql Connection Error: ");
e.printStackTrace();
}
return conn;
}
 
 

Comments