mysq连接 mysql-connector-java-5.1.34.jar

时间:2015-12-07 13:56:07   收藏:0   阅读:776

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 閮戞按娲?
* SQL SERVER 2005杩炴帴
* 绔彛榛樿鏄細1433 杩欓噷鏄?434
*
* 鍦ㄥ唴瀛樹腑锛屽彧闇?鍔犺浇涓?鏁版嵁搴撻┍鍔ㄥ嵆鍙紝鎵?互搴旇鏀惧埌static浠g爜蹇腑 (浼樺寲鎬ц兘)
*/
public class JdbcUtil {

private static String url = "jdbc:mysql://localhost:3306/mysql";
private static String userName = "root";
private static String password ="root";
private static String driverName = "com.mysql.jdbc.Driver";

//1.鍔犺浇鏁版嵁搴撻┍鍔?
static{
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
// TODO: handle exception
e.printStackTrace();
System.out.println("鏁版嵁搴撳姞杞介┍鍔ㄥけ璐?");
}
}

//2.寤虹珛杩炴帴
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.鐢虫槑sql鎵ц鍣?

//4.鎵цSQL 鎿嶄綔鏁版嵁

// 5銆佸叧闂祫婧?
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){//搴旇鐢ㄥ弽灏?
//
// Connection connection = null;
// ResultSet resultSet = null;
// PreparedStatement preparedStatement =null;
//
// try{
// connection = getConnection();
// //SQL鎵ц鍣?
// preparedStatement = connection.prepareStatement(sql);
// resultSet =preparedStatement.executeQuery();
// while (resultSet.next()) {
// resultSet.getString(colum);
// }
// }catch(Exception e){
// e.printStackTrace();
// }finally{
//
// }
// return null;
// }

//query 瀛楁
public static String query(String sql,String colum){//搴旇鐢ㄥ弽灏?

Connection connection = null;
ResultSet resultSet = null;
PreparedStatement preparedStatement =null;
String str ="";

try{
connection = getConnection();
//SQL鎵ц鍣?
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));
}
}

 

原文:http://www.cnblogs.com/zshboke-2015/p/5025698.html

评论(0
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!