当前位置: 技术问答>java相关
如何使用java连接到sql server数据库?任何方法均可!
来源: 互联网 发布时间:2015-08-05
本文导语: rt | 先在数据源设置连接到数据库的名字 就比如此粒子的mrp import java.sql.Connection; import java.sql.SQLException; import java.sql.DriverManager; import java.sql.* ; /** * Title: ConnectionFactory * Desc...
rt
|
先在数据源设置连接到数据库的名字 就比如此粒子的mrp
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() ;
}
}
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() ;
}
}
|
java.sql包里的东西不够吗?
jdbcodbcdriver应该可以呀
jdbcodbcdriver应该可以呀
|
jdbcodbcdriver!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
|
没错jdbcodbcdriver
肯定搞定
肯定搞定