SQL SERVER Connection Standard 2005--sqljdbc4.jar

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.