Import java.sql.Connection;
Import Java.sql.DriverManager;
Import java.sql.PreparedStatement;
Import Java.sql.ResultSet;
Import java.sql.SQLException;
Import java.sql.Statement;
Import java.util.ArrayList;
Import Org.omg.CORBA.PRIVATE_MEMBER;
/**
* @author Zheng
* SQL SERVER 2005 Connection
* Port default is: 1433 this is 1434.
*
* In memory, you need to load the database driver only once, so you should put it in the static code fast (optimize performance)
*/
public class Jdbcutil {
private static String URL = "Jdbc:mysql://localhost:3306/test";
private static String UserName = "root";
private static String password = "root";
private static String drivername = "Com.mysql.jdbc.Driver";
1. Load the database driver
static{
try {
Class.forName ("Com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
Todo:handle exception
E.printstacktrace ();
SYSTEM.OUT.PRINTLN ("Database load driver failed!");
}
}
2. Establish a connection
public static Connection getconnection () {
Connection Connection = null;
try {
Connection = drivermanager.getconnection (URL, userName, password);
} catch (SQLException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
return connection;
}
3. Declare the SQL executor
4. Execute SQL Operation data
5. Close Resources
public static void Free (ResultSet resultset,statement statement,preparedstatement preparedstatement,connection Connection) {
try{
if (resultset!=null) {
Resultset.close ();
}
}catch (SQLException e) {
E.printstacktrace ();
}finally{
try {
if (statement!=null) {
Statement.close ();
}
} catch (SQLException e) {
E.printstacktrace ();
}finally{
try {
if (preparedstatement!=null) {
Preparedstatement.close ();
}
} catch (SQLException e) {
E.printstacktrace ();
}finally{
try {
if (connection!=null) {
Connection.close ();
}
} catch (Exception e) {
E.printstacktrace ();
}
}
}
}
}
Query
public static arraylist<object> query (String sql,string colum) {//should be reflected
//
Connection Connection = null;
ResultSet ResultSet = null;
PreparedStatement PreparedStatement =null;
//
try{
Connection = getconnection ();
SQL Executor
PreparedStatement = connection.preparestatement (sql);
ResultSet =preparedstatement.executequery ();
while (Resultset.next ()) {
Resultset.getstring (Colum);
//}
}catch (Exception e) {
E.printstacktrace ();
}finally{
//
//}
return null;
//}
Query field
public static string query (string sql,string colum) {//should be reflected
Connection Connection = null;
ResultSet ResultSet = null;
PreparedStatement PreparedStatement =null;
String str = "";
try{
Connection = getconnection ();
SQL Executor
PreparedStatement = connection.preparestatement (sql);
ResultSet =preparedstatement.executequery ();
while (Resultset.next ()) {
str = resultset.getstring (colum);
}
}catch (Exception e) {
E.printstacktrace ();
}finally{
}
return str;
}
public static void Main (string[] args) {
String sql = "SELECT * from demo R where R.id=1";
String colum = "name";
System.out.println (Query (Sql,colum));
}
}
SQL SERVER Connection Standard 2005--sqljdbc4.jar