当前位置: 技术问答>java相关
JDBC连接SQL2000的例子,谁有?
来源: 互联网 发布时间:2015-07-26
本文导语: 如何写连接和执行SQl的代码? | String DBDriver = "com.microsoft.jdbc.sqlserver.SQLServerDriver"; //sqlserver://server1:1433", String ConnStr = "jdbc:microsoft:sqlserver://10.47.10.206:1433;DataBaseName=ddd"; Con...
如何写连接和执行SQl的代码?
|
String DBDriver = "com.microsoft.jdbc.sqlserver.SQLServerDriver";
//sqlserver://server1:1433",
String ConnStr = "jdbc:microsoft:sqlserver://10.47.10.206:1433;DataBaseName=ddd";
Connection conn = null;
ResultSet rs = null;
try
{
//注册并连接
java.sql.DriverManager.registerDriver(new com.microsoft.jdbc.sqlserver.SQLServerDriver());
//Class.forName(DBDriver); //也行
//加载数据库驱动程序
System.out.println("Successful!!");
}
catch(Exception e) {
System.out.println("DBrmiconn (): " + e.getMessage());
}
public ResultSet executeQuery(String sql)
{
rs = null;
try
{
conn = DriverManager.getConnection(ConnStr,"sa","mymm");
//与DBMS建立连接
Statement stmt = conn.createStatement();
rs = stmt.executeQuery(sql);
System.out.println("Connection is correct!");
}
catch(SQLException ex) {
System.out.println("aq.executeQuery: " + ex.getMessage());
}
return rs;
}
public boolean executeUpdate(String sql)
{
boolean b=false;
try
{
conn=DriverManager.getConnection(ConnStr);
Statement stmt=conn.createStatement();
stmt.executeUpdate(sql);
b=true;
}
catch(SQLException ex){
System.out.println("aq.executeUpdate:" + ex.getMessage());
}
注意:你的有SQL2000的jdbc 驱动程序。
我可以发个你。三个jar。
//sqlserver://server1:1433",
String ConnStr = "jdbc:microsoft:sqlserver://10.47.10.206:1433;DataBaseName=ddd";
Connection conn = null;
ResultSet rs = null;
try
{
//注册并连接
java.sql.DriverManager.registerDriver(new com.microsoft.jdbc.sqlserver.SQLServerDriver());
//Class.forName(DBDriver); //也行
//加载数据库驱动程序
System.out.println("Successful!!");
}
catch(Exception e) {
System.out.println("DBrmiconn (): " + e.getMessage());
}
public ResultSet executeQuery(String sql)
{
rs = null;
try
{
conn = DriverManager.getConnection(ConnStr,"sa","mymm");
//与DBMS建立连接
Statement stmt = conn.createStatement();
rs = stmt.executeQuery(sql);
System.out.println("Connection is correct!");
}
catch(SQLException ex) {
System.out.println("aq.executeQuery: " + ex.getMessage());
}
return rs;
}
public boolean executeUpdate(String sql)
{
boolean b=false;
try
{
conn=DriverManager.getConnection(ConnStr);
Statement stmt=conn.createStatement();
stmt.executeUpdate(sql);
b=true;
}
catch(SQLException ex){
System.out.println("aq.executeUpdate:" + ex.getMessage());
}
注意:你的有SQL2000的jdbc 驱动程序。
我可以发个你。三个jar。
|
=============MySQL=================================
Class.forName("org.gjt.mm.mysql.Driver").newInstance();
conn = DriverManager.getConnection("jdbc:mysql://myhost:3306/dbname","user","passwd");
Class.forName("org.gjt.mm.mysql.Driver").newInstance();
conn = DriverManager.getConnection("jdbc:mysql://myhost:3306/dbname","user","passwd");
|
public void openConnection() throws ClassNotFoundException,SQLException {
this.conn=DriverManager.getConnection( "jdbc:odbc:数据原;","用户名","密码");
}
this.conn=DriverManager.getConnection( "jdbc:odbc:数据原;","用户名","密码");
}