package cn.test.merlini;
import java.sql.*;
public class DbSample {
/**
* @param args
*/
public Connection dbConnection =null;
private String driverName;
private String host;
private String port;
private String dbName;
private String userName;
private String password;
private String enCoding;
public DbSample(String host,String port,String dbName,String userName,String password)
{
this.driverName="com.mysql.jdbc.Driver";
this.host=host;
this.port=port;
this.dbName=dbName;
this.userName=userName;
this.password=password;
this.enCoding="useUnicode=true&characterEncoding=utf8";
}
public String getURL()
{
StringBuilder url=new StringBuilder();
url.append("jdbc:mysql://");
url.append(host);
url.append(":");
url.append(port);
url.append("/");
url.append(dbName);
url.append("?");
url.append("user="+userName);
url.append("&");
url.append("password="+password);
url.append("&");
url.append(enCoding);
return url.toString();
}
public boolean connectionDB(String url)
{
try
{
Class.forName(this.driverName);
// System.out.println(url);
dbConnection=DriverManager.getConnection(url);
System.out.println("Mysql connect success");
return true;
}catch(Exception e){
System.out.println("Mysql connect000 failed");
e.getStackTrace();
return false;
}
}
public boolean closeDB()
{
try
{
if(dbConnection !=null)
{
dbConnection.close();
System.out.println("close ok");
}
return true;
}catch(Exception e)
{
System.out.println("Mysql close failed");
e.getStackTrace();
return false;
}
}
public boolean doSelect(String sql)
{
Statement statement =null;
ResultSet result =null;
try
{
statement = dbConnection.createStatement();
result = statement.executeQuery(sql);
while(result.next())
{
System.out.print(result.getInt(1)+" ");
System.out.println(result.getString(2));
}
return true;
}catch(Exception e)
{
System.out.println("查询失败");
e.getStackTrace();
return false;
}finally
{
try
{
if(result !=null)
result.close();
if(statement !=null)
statement.close();
}catch(SQLException e)
{
e.printStackTrace();
}
}
}
public boolean doUpdate(String sql)
{
Statement statement =null;
try
{
statement = dbConnection.createStatement();
statement.executeUpdate(sql);
return true;
}catch(Exception e)
{
System.out.println("insert操作失败");
e.getStackTrace();
return false;
}finally
{
try
{
if(statement !=null)
statement.close();
}catch(SQLException e)
{
e.printStackTrace();
}
}
}
public static void main(String[] args)
{
String sql="";
DbSample obj=new DbSample("localhost","3306","test","admin","123456");
obj.connectionDB(obj.getURL());
sql="DROP TABLE IF EXISTS `student`;";
obj.doUpdate(sql);
sql="CREATE TABLE `student` (`NO` int(11) , `name` VARCHAR(20) DEFAULT NULL) ENGINE=INNODB DEFAULT CHARSET=utf8;";
obj.doUpdate(sql);
obj.doUpdate("insert into student(NO,name) values(1,'孤鸿影')");
obj.doUpdate("insert into student(No,name) values(2,'孤鸿影1')");
obj.doSelect("select * from student");
try
{
PreparedStatement newStudent = null;
newStudent=obj.dbConnection.prepareStatement("insert into student values(?,?)");
newStudent.setInt(1, 1);
newStudent.setString(2,"爱我中华");
newStudent.executeUpdate();
newStudent.clearParameters();
newStudent.setInt(1, 1);
newStudent.setString(2,"爱我中华");
newStudent.executeUpdate();
newStudent.clearParameters();
newStudent.close();
}catch(SQLException e)
{
e.printStackTrace();
}
System.out.println("使用prepareStatement更新数据后");
obj.doSelect("select * from student");
obj.closeDB();
}
}