java mysql链接
2013-08-13 00:30
337 查看
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)+"\t");
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();
}
}
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)+"\t");
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();
}
}
相关文章推荐
- java链接Mysql异常:com.mysql.jdbc.exceptions.jdbc4.CommunicationsException:
- JAVA链接Mysql
- java实现兼容Oracle、mysql、SQL server数据库的链接
- java 链接mysql
- Java jdbc 链接数据库MYSQL问题
- tomcat链接mysql时超时报错java.io.EOFException: Can not read response from server. Expected to read 4 bytes,
- Java学习之路:2、Mysql 链接与查询
- java链接数据库--Mysql
- web网页Eclipse,jsp+Servlet+javaBean,访问Mysql链接数据库
- tomcat链接mysql时超时报错java.io.EOFException: Can not read response from server. Expected to read 4 bytes,
- Java链接MySQL时遇到的问题
- Mysql学习第一章 Java链接Mysql
- Java jdbc链接MySQL
- java通过JDBC链接mysql报错解决办法
- eclipse中java链接mysql步骤
- Java使用jdbc链接数据库的MySQL 5.5.45+以及SSL错误解决办法
- linux上java项目链接不上mysql,本地测试无问题
- java链接mysql
- Java链接MySQL乱码解决
- java.lang.ClassNotFoundException: com.mysql.jdbc.Driver(Servlet链接mysql数据库失败解决方法)