当前位置: 技术问答>java相关
java怎样联接数据库
来源: 互联网 发布时间:2015-10-30
本文导语: 在jbuilder6中,怎样和远程的sql server联接 | package jsp; import java.sql.*; // JDBC package public class sql_data { String url = "jdbc:inetdae:192.168.2.70?sql7=true"; // use your hostname and port number String login = "sa"; //...
在jbuilder6中,怎样和远程的sql server联接
|
package jsp;
import java.sql.*; // JDBC package
public class sql_data {
String url = "jdbc:inetdae:192.168.2.70?sql7=true"; // use your hostname and port number
String login = "sa"; // use your login here
String password =""; // use your password here
public Connection connection = null;
public Statement st = null;
public ResultSet rs = null;
public sql_data(){
try {
Class.forName("com.inet.tds.TdsDriver").newInstance();
DriverManager.setLoginTimeout(10);
} catch(Exception e) {
e.printStackTrace();
}
}
public void sqlclose() {
try {
st.close();
connection.close();
}
catch(SQLException ex) {
System.err.println("sqlclose: " + ex.getMessage());
}
}
public ResultSet executeQuery(String sql) {
try {
connection = DriverManager.getConnection(url,login,password);
connection.setCatalog( "register");
st = connection.createStatement();
rs = st.executeQuery(sql);
}
catch(SQLException ex) {
System.err.println("aq.executeQuery: " + ex.getMessage());
}
return rs;
}
}
import java.sql.*; // JDBC package
public class sql_data {
String url = "jdbc:inetdae:192.168.2.70?sql7=true"; // use your hostname and port number
String login = "sa"; // use your login here
String password =""; // use your password here
public Connection connection = null;
public Statement st = null;
public ResultSet rs = null;
public sql_data(){
try {
Class.forName("com.inet.tds.TdsDriver").newInstance();
DriverManager.setLoginTimeout(10);
} catch(Exception e) {
e.printStackTrace();
}
}
public void sqlclose() {
try {
st.close();
connection.close();
}
catch(SQLException ex) {
System.err.println("sqlclose: " + ex.getMessage());
}
}
public ResultSet executeQuery(String sql) {
try {
connection = DriverManager.getConnection(url,login,password);
connection.setCatalog( "register");
st = connection.createStatement();
rs = st.executeQuery(sql);
}
catch(SQLException ex) {
System.err.println("aq.executeQuery: " + ex.getMessage());
}
return rs;
}
}
|
楼上的方法可行!!
还有这个
import java.io.*;
import java.util.*;
import java.sql.*;
import java.lang.*;
public class DBConnect {
private String jdbcDriver=null;
private String jdbcURL=null;
private String userName=null;
private String password=null;
public DBConnect() {
jdbcDriver="sun.jdbc.odbc.JdbcOdbcDriver";
jdbcURL="jdbc:odbc:shihong";
userName="system ";
password="manager";
}
public Connection getConnection(){
Connection connection=null;
try{
Class.forName(jdbcDriver);
connection=DriverManager.getConnection(jdbcURL,userName,password);
}catch(Exception e){
System.out.println(e);
}
return connection;
}
}
还有这个
import java.io.*;
import java.util.*;
import java.sql.*;
import java.lang.*;
public class DBConnect {
private String jdbcDriver=null;
private String jdbcURL=null;
private String userName=null;
private String password=null;
public DBConnect() {
jdbcDriver="sun.jdbc.odbc.JdbcOdbcDriver";
jdbcURL="jdbc:odbc:shihong";
userName="system ";
password="manager";
}
public Connection getConnection(){
Connection connection=null;
try{
Class.forName(jdbcDriver);
connection=DriverManager.getConnection(jdbcURL,userName,password);
}catch(Exception e){
System.out.println(e);
}
return connection;
}
}