package baodian.util;
import java.util.*;
import java.sql.*;
import java.io.*;
public class ExecuteSQL
{
static Connection con=null;
ResultSet rs=null;
//load JDBC driver
public ExecuteSQL()
{
try
{
Class.forName("com.mysql.jdbc.Driver");
//Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver").newInstance();
}
catch(ClassNotFoundException e)
{
System.err.println(e.getMessage());
}
catch(Exception e)
{
System.err.println(e);
}
}
//return query Resultret
public ResultSet executeQuery(String sql) throws Exception
{
try
{
con=getConnection();
Statement stmt=con.createStatement();
rs=stmt.executeQuery(sql);
}
catch(SQLException e)
{
System.err.println(e.getMessage());
}
return rs;
}
/*
public void executeUpdate(String sql)
{
try
{
con=getConnection();
Statement stmt=con.createStatement();
stmt.executeUpdate(sql);
}
catch(SQLException e)
{
System.err.println(e.getMessage());
}
}
*/
public int executeUpdate(String sql) throws Exception
{
int num = 0;
try
{
con = getConnection();
Statement stmt = con.createStatement();
num = stmt.executeUpdate(sql);
}
catch(SQLException e)
{
System.err.println(e.getMessage());
}
return num;
}
public static Connection getConnection() throws SQLException
{
String url="jdbc:mysql://127.0.0.1/baodian?useUnicode=true&characterEncoding=UTF8";
String username="root";
String password="";
try
{
con=java.sql.DriverManager.getConnection(url, username, password);
}
catch(SQLException e)
{
System.err.println(e);
}
return con;
}
//close database
public void closeDB() throws SQLException
{
try
{
if (rs!=null)
{
rs.close();
}
if(con!=null)
{
con.close();
}
}
catch(SQLException e)
{
System.err.println(e.getMessage());
}
}
public static String TransUTF8(String chi)
{
String result="";
try
{
result=new String(chi.getBytes("ISO8859_1"),"UTF-8");
}
catch (UnsupportedEncodingException e)
{
System.out.println(e.toString());
}
return result;
}
public static String TransISO(String chi)
{
String result="";
try
{
result = new String(chi.getBytes("UTF-8"),"ISO8859_1");
}
catch (UnsupportedEncodingException e)
{
System.out.println(e.toString());
}
return result;
}
}