Sunday, April 19, 2009

How to connect MySql Server using JDBC

还没有试过呢,先粘上来再说。
How to connect MySql Server using JDBC
http://www.java-tips.org/other-api-tips/jdbc/how-to-connect-mysql-server-using-jdbc.html

User Rating: / 60
PoorBest
mysql-connector-java-3.1.10 is JDBC connector for MYSQL database. Place the mysql-connector-java-3.1.10-bin.jar file in your CLASSPATH variable or Application folder.

Example below makes connection to mySQL using com.mysql.jdbc.Driver.




import java.sql.*;
public class getmySQLConnection
{
public static void main(String[] args)
{
DB db = new DB();
Connection conn = db.dbConnect(
"jdbc:mysql://localhost:3306/test", "root", "");
}
}

class DB
{
public DB() {}

public Connection dbConnect(String db_connect_string,
String db_userid, String db_password)
{
try
{
Class.forName("com.mysql.jdbc.Driver").newInstance();
Connection conn = DriverManager.getConnection(
db_connect_string, db_userid, db_password);

System.out.println("connected");
return conn;

}
catch (Exception e)
{
e.printStackTrace();
return null;
}
}
};   // end class DB

No comments:

Post a Comment