2个回答
2013-09-06
展开全部
/*
* Created on 2004-6-3
*
* 连接数据库的相关操作
*
*/
package connect;
/**
* @author Ambrosini
*
* To change the template for this generated type comment go to
* Window>Preferences>Java>Code Generation>Code and Comments
*/
import java.sql.*;
import java.util.*;
public class DataConnect {
String driverName = "sun.jdbc.odbc.JdbcOdbcDriver";
String databaseURL = "jdbc:odbc:Test";
String usr = "Test";
String psword = "Test";
Vector vector;
Connection connection=null;
Statement statement=null;
ResultSet resultSet=null;
ResultSetMetaData rsMetaData=null;
//连接数据库
public DataConnect(){
vector =new Vector();
try{
Class.forName(driverName);
}catch(ClassNotFoundException e){System.err.println(e.getMessage());}
}
//返回结果集
public ResultSet getResulet(String sql){
String sql2000 = sql ;
try{
connection = DriverManager.getConnection(databaseURL,usr,psword);
statement = connection.createStatement();
resultSet = statement.executeQuery(sql2000);
rsMetaData = resultSet.getMetaData();
}catch(SQLException ee){System.err.println(ee.getMessage());}
return resultSet;
}
//关闭连接
public void shutDown() {
try{
connection.close();
}catch(SQLException ex){System.err.println(ex.getMessage());}
}
//更改数据库操作
public void executeUpdate(String sql) {
try {
connection = DriverManager.getConnection(databaseURL,usr,psword);
statement = connection.createStatement();
statement.executeUpdate(sql);
}
catch(SQLException ee) {
System.err.println(ee.getMessage());
}
}
}
* Created on 2004-6-3
*
* 连接数据库的相关操作
*
*/
package connect;
/**
* @author Ambrosini
*
* To change the template for this generated type comment go to
* Window>Preferences>Java>Code Generation>Code and Comments
*/
import java.sql.*;
import java.util.*;
public class DataConnect {
String driverName = "sun.jdbc.odbc.JdbcOdbcDriver";
String databaseURL = "jdbc:odbc:Test";
String usr = "Test";
String psword = "Test";
Vector vector;
Connection connection=null;
Statement statement=null;
ResultSet resultSet=null;
ResultSetMetaData rsMetaData=null;
//连接数据库
public DataConnect(){
vector =new Vector();
try{
Class.forName(driverName);
}catch(ClassNotFoundException e){System.err.println(e.getMessage());}
}
//返回结果集
public ResultSet getResulet(String sql){
String sql2000 = sql ;
try{
connection = DriverManager.getConnection(databaseURL,usr,psword);
statement = connection.createStatement();
resultSet = statement.executeQuery(sql2000);
rsMetaData = resultSet.getMetaData();
}catch(SQLException ee){System.err.println(ee.getMessage());}
return resultSet;
}
//关闭连接
public void shutDown() {
try{
connection.close();
}catch(SQLException ex){System.err.println(ex.getMessage());}
}
//更改数据库操作
public void executeUpdate(String sql) {
try {
connection = DriverManager.getConnection(databaseURL,usr,psword);
statement = connection.createStatement();
statement.executeUpdate(sql);
}
catch(SQLException ee) {
System.err.println(ee.getMessage());
}
}
}
2018-09-08 · 百度知道合伙人官方认证企业
关注
展开全部
public static void main(String arg[]) {
try {
Connection con = null; //定义一个MYSQL链接对象
Class.forName("com.mysql.jdbc.Driver").newInstance(); //MYSQL驱动
con = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/test", "root", "root"); //链接本地MYSQL
System.out.print("yes");
} catch (Exception e) {
}
try {
Connection con = null; //定义一个MYSQL链接对象
Class.forName("com.mysql.jdbc.Driver").newInstance(); //MYSQL驱动
con = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/test", "root", "root"); //链接本地MYSQL
System.out.print("yes");
} catch (Exception e) {
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询