当前位置: 技术问答>java相关
简单问题,解决即给分
来源: 互联网 发布时间:2015-09-06
本文导语: import java.sql.*; public class b{ public static void main(String[] args) { try { Class.forName("oracle.jdbc.driver.OracleDriver"); //check the driver by trying to get a connection Connection conn = ...
import java.sql.*;
public class b{
public static void main(String[] args) {
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
//check the driver by trying to get a connection
Connection conn =
DriverManager.getConnection("jdbc:oracle:thin:@japan-server:1521:prospec", "scott","tiger");
if (conn != null) {
System.out.println("Driver loaded: Connection made to db.");
} else {
System.out.println("NO Connection to db. Driver problem.");
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
这是oracle连接方法,我想连接sql server 库名:dbsql 机器名:linyaliang
用户名:sa 密码:空,怎么写
public class b{
public static void main(String[] args) {
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
//check the driver by trying to get a connection
Connection conn =
DriverManager.getConnection("jdbc:oracle:thin:@japan-server:1521:prospec", "scott","tiger");
if (conn != null) {
System.out.println("Driver loaded: Connection made to db.");
} else {
System.out.println("NO Connection to db. Driver problem.");
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
这是oracle连接方法,我想连接sql server 库名:dbsql 机器名:linyaliang
用户名:sa 密码:空,怎么写
|
//你必须先装JDBC再用:
import java.sql.*;
public class b{
public static void main(String[] args) {
try {
//Class.forName("oracle.jdbc.driver.OracleDriver");
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
//check the driver by trying to get a connection
//Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@japan-server:1521:prospec", "scott","tiger");
Connection conn = DriverManager.getConnection("jdbc:microsoft:sqlserver://japan-server:1433;DatabaseName=Test","sa","");
if (conn != null) {
System.out.println("Driver loaded: Connection made to db.");
} else {
System.out.println("NO Connection to db. Driver problem.");
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
import java.sql.*;
public class b{
public static void main(String[] args) {
try {
//Class.forName("oracle.jdbc.driver.OracleDriver");
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
//check the driver by trying to get a connection
//Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@japan-server:1521:prospec", "scott","tiger");
Connection conn = DriverManager.getConnection("jdbc:microsoft:sqlserver://japan-server:1433;DatabaseName=Test","sa","");
if (conn != null) {
System.out.println("Driver loaded: Connection made to db.");
} else {
System.out.println("NO Connection to db. Driver problem.");
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
|
import java.sql.*;
import java.lang.*;
import java.io.*;
public class b{
public static void main(String[] args){
try{
String sDBDriver = "sun.jdbc.odbc.JdbcOdbcDriver";
String sConnStr = "jdbc:odbc:dbsql";
Connection conn = null;
conn = DriverManager.getConnection(sConnStr,"sa","");
}
catch(Exception e){
System.out.println("error:");
}
}
}
别忘了,建ODBC数据源
import java.lang.*;
import java.io.*;
public class b{
public static void main(String[] args){
try{
String sDBDriver = "sun.jdbc.odbc.JdbcOdbcDriver";
String sConnStr = "jdbc:odbc:dbsql";
Connection conn = null;
conn = DriverManager.getConnection(sConnStr,"sa","");
}
catch(Exception e){
System.out.println("error:");
}
}
}
别忘了,建ODBC数据源
|
jdbc驱动设置(非jbuilder的运行环境下,比如cmd环境):
下载一个驱动,安装后将驱动文件的绝对路径放入classpath环境变量里面去,最好将驱动就安装到jdk的lib目录里面算了。
这方面设置若有问题参考:http://www.csdn.net/expert/topic/750/750375.xml?temp=.4715387
JBuilder连接数据库的jdbc驱动设置:
1.在tools->configre libraries->new->键入name->add->选择驱动文件。不妨设置在User Home下面
2.tools->enterprise setup->database Drier->add->选择刚才新建的
3.tools->database pilot->file->new->键入:url和Driver
4.双击->输入数据库的用户名和密码
可运行代码实例:
import java.sql.*;
public class Test {
public static void main(String[] args) {
try {
String address = "jdbc:microsoft:sqlserver://192.168.0.24:1433";
//驱动类型+目标数据库ip+数据库端口
String user="sa";//数据库用户密码
String passwd="";//口令
String database = "TESTDB";//目标数据库
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");//驱动申明
Connection con = DriverManager.getConnection(address,user,passwd);//建立链接
con.setCatalog(database);//确定目标数据库
Statement smt = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY);
//建立描述,设定结果集支持滚动光标且敏感,不可编辑
String selCode = "SELECT userId,userName FROM UserTable";//查询语句
ResultSet rs = smt.executeQuery(selCode);//结果集
if(rs.last()) {//从第一条往后依次取结果集中的记录
String userId = rs.getString(1);//等同rs.getString("userId"),即第一个字段数据
String userName = rs.getString(2);//同上,第二个字段,全部取其为String类型
System.out.println(userId+":"+userName);//输出此条记录的查询结果
}
rs.close();//释放资源
smt.close();
con.close();
}
catch(Exception e) {
System.out.println(e);//捕捉异常
}
}
}
下载一个驱动,安装后将驱动文件的绝对路径放入classpath环境变量里面去,最好将驱动就安装到jdk的lib目录里面算了。
这方面设置若有问题参考:http://www.csdn.net/expert/topic/750/750375.xml?temp=.4715387
JBuilder连接数据库的jdbc驱动设置:
1.在tools->configre libraries->new->键入name->add->选择驱动文件。不妨设置在User Home下面
2.tools->enterprise setup->database Drier->add->选择刚才新建的
3.tools->database pilot->file->new->键入:url和Driver
4.双击->输入数据库的用户名和密码
可运行代码实例:
import java.sql.*;
public class Test {
public static void main(String[] args) {
try {
String address = "jdbc:microsoft:sqlserver://192.168.0.24:1433";
//驱动类型+目标数据库ip+数据库端口
String user="sa";//数据库用户密码
String passwd="";//口令
String database = "TESTDB";//目标数据库
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");//驱动申明
Connection con = DriverManager.getConnection(address,user,passwd);//建立链接
con.setCatalog(database);//确定目标数据库
Statement smt = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY);
//建立描述,设定结果集支持滚动光标且敏感,不可编辑
String selCode = "SELECT userId,userName FROM UserTable";//查询语句
ResultSet rs = smt.executeQuery(selCode);//结果集
if(rs.last()) {//从第一条往后依次取结果集中的记录
String userId = rs.getString(1);//等同rs.getString("userId"),即第一个字段数据
String userName = rs.getString(2);//同上,第二个字段,全部取其为String类型
System.out.println(userId+":"+userName);//输出此条记录的查询结果
}
rs.close();//释放资源
smt.close();
con.close();
}
catch(Exception e) {
System.out.println(e);//捕捉异常
}
}
}
|
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
//check the driver by trying to get a connection
Connection conn =
DriverManager.getConnection("jdbc:odbc:dsn", "sa","");
给你的sql server建个系统数据源,名为dsn
|
jdbc:odbc:testAccess
DriverManager.getConnection("jdbc:odbc:testSQL
", "sa","");
testSQL用odbc工具配置!!
呵呵
DriverManager.getConnection("jdbc:odbc:testSQL
", "sa","");
testSQL用odbc工具配置!!
呵呵
|
String url = "202.38.64.5";
String UserName = "sa";
String UserPassword = "";
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
Connection conn = DriverManager.getConnection("jdbc:microsoft:dbsql://"+url+":1433",UserName,UserPassword);
String UserName = "sa";
String UserPassword = "";
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
Connection conn = DriverManager.getConnection("jdbc:microsoft:dbsql://"+url+":1433",UserName,UserPassword);
|
Class.forName(); 这个方法所载入的数据库驱动类. 如果是要连接 sql server, 当然你得先找到相应的 sql server 的驱动,至于驱动的 url 是针对具体的驱动来写的,不同的驱动可能稍有区别.看看这个帖子:
http://www.csdn.net/Expert/TopicView1.asp?id=1025784
http://www.csdn.net/Expert/TopicView1.asp?id=1025784