Thursday, September 2, 2010

Java MySQL Connection

MySQL is a great and powerful database and its combination with JAVA make is much more powerful. I would not go in long stories, i would tell you simple how to make a JDBC connection between JAVA and MySQL. for that you need java and mysql (of course) and MySQL JConnector (download it). once you download the jar file put it in the classpath of you project and use the following code to make connection and thats all you successfully make a JAVA MySQL connection.

import java.sql.*;
public class DBClass {
private Statement getStatement(){
Statement st=null;
try{
Class.forName("com.mysql.jdbc.Driver").newInstance();
Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/DB_NAME", "USER_NAME", "SECRET_PASSWORD");
st=con.createStatement();
}
catch(Exception d){
System.out.println(d);
}
return st;
}

public ResultSet rs(String q){
ResultSet rs=null;
try{
rs=getStatement().executeQuery(q);
}
catch(Exception d){
System.out.println(d);
}
return rs;
}
public int updateQuery(String q){
int k = 0;
try{
k=getStatement().executeUpdate(q);
}
catch(Exception d){
System.out.println(d);
}
return k;
}
}

0 comments:

Post a Comment

 

Blog Info

A Pakistani Website by Originative Systems

Total Pageviews

Tutorial Jinni Copyright © 2015 WoodMag is Modified by Originative Systems