如何使用java连接到sql server数据库?任何方法均可! java.sql包里的东西不够吗?jdbcodbcdriver应该可以呀 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 先在数据源设置连接到数据库的名字 就比如此粒子的mrpimport java.sql.Connection;import java.sql.SQLException;import java.sql.DriverManager;import java.sql.* ;/** * Title: ConnectionFactory * Description: The class used to get a connection by the given params * Copyright: Copyright (c) 2002 * Company: css * @author Dreamingmouse * @version 1.0 *//** * this class is responsible for getting a Connection by the given params */public class ConnectionFactory { public static final String URL = "jdbc:odbc:mrp" ; public static final String DRIVER = "sun.jdbc.odbc.JdbcOdbcDriver" ; public static final String USER = "sa" ; public static final String PASSWORD = "" ; public String url ; public String driver ; public String user ; public String password ; public ConnectionFactory() { //do nothing } public ConnectionFactory( String url , String driver , String user , String password ) { this.url = url ; this.driver = driver ; this.user = user ; this.password = password ; } public Connection getConnection () { try{ Class.forName( driver ) ; Connection con = DriverManager.getConnection( url , user , password ) ; return con ; }catch( SQLException se ) { return null ; }catch( ClassNotFoundException cnfe ) { return null ; } } public static Connection getDefaultConnection() { try{ Class.forName( DRIVER ) ; Connection con = DriverManager.getConnection( URL , USER , PASSWORD ) ; return con ; }catch( SQLException se ) { return null ; }catch( ClassNotFoundException cnfe ) { return null ; } } public static void main(String[] args) throws Exception { String url = "jdbc:odbc:liaocheng" ; //this is a local dsn on local platform . //change it to run anywhere else String driver = "sun.jdbc.odbc.JdbcOdbcDriver" ; String user = "sa" ; String password = "" ; ConnectionFactory connectionFactory1 = new ConnectionFactory( url , driver ,user,password); Connection con = connectionFactory1.getConnection() ; PreparedStatement ps = con.prepareStatement("select * from lc_news") ; ResultSet rs = ps.executeQuery() ; System.out.println(rs.getStatement()); int i = 0; while(rs.next()){; System.out.println("+++++++++++++++++"); System.out.println(rs.getString(2)); System.out.println(rs.getString(3)); System.out.println(rs.getString("message")); i++; } con.close() ; }} jdbcodbcdriver!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! 没错jdbcodbcdriver肯定搞定 也可以用SQLSERVER专用的JDBC驱动程序!!!!!!!!!!!!!!!!! JFrame对象与垃圾回收问题 求字数统计代码 怎么经常说找不到符号啊? 新人问题:书上关于访问控制符(Access Control)的一句晦涩难懂的话 Log4j 的 HTMLLayout 问题? 输出类名时后面跟了奇怪的尾巴?怎么会事! 一个菜鸟的问题 ! 关于PrintWriter.close() 那里可以下载到中文语音输入软件(如 ibm的语音输入系统)!!! 又白又嫩的菜鸟问题? Jbuilder 菜鸟问题 各位大哥,请问java web start 怎么应用呀,
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.DriverManager;
import java.sql.* ;
/**
* Title: ConnectionFactory
* Description: The class used to get a connection by the given params
* Copyright: Copyright (c) 2002
* Company: css
* @author Dreamingmouse
* @version 1.0
*/
/**
* this class is responsible for getting a Connection by the given params
*/
public class ConnectionFactory {
public static final String URL = "jdbc:odbc:mrp" ;
public static final String DRIVER = "sun.jdbc.odbc.JdbcOdbcDriver" ;
public static final String USER = "sa" ;
public static final String PASSWORD = "" ;
public String url ;
public String driver ;
public String user ;
public String password ; public ConnectionFactory() {
//do nothing
} public ConnectionFactory( String url , String driver , String user , String password ) {
this.url = url ;
this.driver = driver ;
this.user = user ;
this.password = password ;
} public Connection getConnection () {
try{
Class.forName( driver ) ;
Connection con = DriverManager.getConnection( url , user , password ) ;
return con ;
}catch( SQLException se ) {
return null ;
}catch( ClassNotFoundException cnfe ) {
return null ;
}
}
public static Connection getDefaultConnection() {
try{
Class.forName( DRIVER ) ;
Connection con = DriverManager.getConnection( URL , USER , PASSWORD ) ;
return con ;
}catch( SQLException se ) {
return null ;
}catch( ClassNotFoundException cnfe ) {
return null ;
}
}
public static void main(String[] args) throws Exception {
String url = "jdbc:odbc:liaocheng" ; //this is a local dsn on local platform .
//change it to run anywhere else
String driver = "sun.jdbc.odbc.JdbcOdbcDriver" ;
String user = "sa" ;
String password = "" ;
ConnectionFactory connectionFactory1 = new ConnectionFactory( url , driver ,user,password);
Connection con = connectionFactory1.getConnection() ;
PreparedStatement ps = con.prepareStatement("select * from lc_news") ;
ResultSet rs = ps.executeQuery() ;
System.out.println(rs.getStatement());
int i = 0;
while(rs.next()){;
System.out.println("+++++++++++++++++");
System.out.println(rs.getString(2));
System.out.println(rs.getString(3));
System.out.println(rs.getString("message"));
i++;
}
con.close() ;
}
}
肯定搞定