javaweb连接sql、oracle的详细过程和用法
4个回答
展开全部
---------声明:这个是复制来的
先建立数据源,开始,管理工具,数据源odbc,里面应该可以创建了。选择上你要建立数据源的数据库,下面的dms就是起得数据源名字,要记住。代码如下:
Connection con=null;
java.sql.Statement st=null;
ResultSet rs=null;
String Source="DMS";
String user=null,pass=null;
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con=DriverManager.getConnection
("jdbc:odbc:"+Source,user,pass);
st=con.createStatement();
rs=st.executeQuery(str);
对于oracle,先要从oracle安装目录中搜索到ojdbc14.jar文件,然后加入到你的工程的lib中去,看具体情况,有开发工具区别。代码如下,里面的ip地址中其实已经包含了端口号,比如92.168.1.1:1521,一般情况下端口号就是1521.
public boolean open()
{
Properties props = new Properties();
props.put("user", username);
props.put("password", password);
Driver myDriver=null;
try {
myDriver = (Driver) Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
} catch (InstantiationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return false;
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return false;
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return false;
}
try {
System.out.println("jdbc:oracle:thin:@"+this.IPAdress+":"+this.database);
conn = myDriver.connect("jdbc:oracle:thin:@"+this.IPAdress+":"+this.database, props);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return false;
}
try {
st=conn.createStatement();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return false;
}
return true;
}
先建立数据源,开始,管理工具,数据源odbc,里面应该可以创建了。选择上你要建立数据源的数据库,下面的dms就是起得数据源名字,要记住。代码如下:
Connection con=null;
java.sql.Statement st=null;
ResultSet rs=null;
String Source="DMS";
String user=null,pass=null;
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con=DriverManager.getConnection
("jdbc:odbc:"+Source,user,pass);
st=con.createStatement();
rs=st.executeQuery(str);
对于oracle,先要从oracle安装目录中搜索到ojdbc14.jar文件,然后加入到你的工程的lib中去,看具体情况,有开发工具区别。代码如下,里面的ip地址中其实已经包含了端口号,比如92.168.1.1:1521,一般情况下端口号就是1521.
public boolean open()
{
Properties props = new Properties();
props.put("user", username);
props.put("password", password);
Driver myDriver=null;
try {
myDriver = (Driver) Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
} catch (InstantiationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return false;
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return false;
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return false;
}
try {
System.out.println("jdbc:oracle:thin:@"+this.IPAdress+":"+this.database);
conn = myDriver.connect("jdbc:oracle:thin:@"+this.IPAdress+":"+this.database, props);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return false;
}
try {
st=conn.createStatement();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return false;
}
return true;
}
展开全部
import java.sql.*;
public class TestOracle{
public static void main (String args[]){
try{
Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
String url = "jdbc:oracle:thin:@localhost:1521:binbo";
String user ="scott";
String password = "tiger";
Connection conn = DriverManager.getConnection(url,user,password);
Statement stat = conn.createStatement();
ResultSet result = stat.executeQuery("SELECT * FROM test");
result.next();
System.out.println(result.getString(1));
System.out.println(result.getString(2));
System.out.println(result.getString(3));
result.close();
stat.close();
conn.close();
}catch(ClassNotFoundException en){
System.out.println("数据库驱动找不到!");
en.printStackTrace();
}catch(SQLException ex) {
while (ex != null) {
ex.printStackTrace();
ex = ex.getNextException();
}
}catch(Exception e){
System.out.println("其他未知异常!");
e.printStackTrace();
}
}
}
SQL SERVER
import java.sql.*;
import java.util.*;
public class TestSQLServer{
public static void main (String args[]){
try{
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver").newInstance();
String url = "jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=testdb";
String user ="sa";
String password = "sa";
Connection conn = DriverManager.getConnection(url,user,password);
Statement stat = conn.createStatement();
ResultSet result = stat.executeQuery("SELECT * FROM users");
result.next();
System.out.println(result.getString(1));
System.out.println(result.getString(2));
System.out.println(result.getString(3));
result.close();
stat.close();
conn.close();
}catch(ClassNotFoundException en){
System.out.println("数据库驱动找不到!");
en.printStackTrace();
}catch(SQLException ex) {
while (ex != null) {
ex.printStackTrace();
ex = ex.getNextException();
}
}catch(Exception e){
System.out.println("其他未知异常!");
e.printStackTrace();
}
}
}
public class TestOracle{
public static void main (String args[]){
try{
Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
String url = "jdbc:oracle:thin:@localhost:1521:binbo";
String user ="scott";
String password = "tiger";
Connection conn = DriverManager.getConnection(url,user,password);
Statement stat = conn.createStatement();
ResultSet result = stat.executeQuery("SELECT * FROM test");
result.next();
System.out.println(result.getString(1));
System.out.println(result.getString(2));
System.out.println(result.getString(3));
result.close();
stat.close();
conn.close();
}catch(ClassNotFoundException en){
System.out.println("数据库驱动找不到!");
en.printStackTrace();
}catch(SQLException ex) {
while (ex != null) {
ex.printStackTrace();
ex = ex.getNextException();
}
}catch(Exception e){
System.out.println("其他未知异常!");
e.printStackTrace();
}
}
}
SQL SERVER
import java.sql.*;
import java.util.*;
public class TestSQLServer{
public static void main (String args[]){
try{
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver").newInstance();
String url = "jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=testdb";
String user ="sa";
String password = "sa";
Connection conn = DriverManager.getConnection(url,user,password);
Statement stat = conn.createStatement();
ResultSet result = stat.executeQuery("SELECT * FROM users");
result.next();
System.out.println(result.getString(1));
System.out.println(result.getString(2));
System.out.println(result.getString(3));
result.close();
stat.close();
conn.close();
}catch(ClassNotFoundException en){
System.out.println("数据库驱动找不到!");
en.printStackTrace();
}catch(SQLException ex) {
while (ex != null) {
ex.printStackTrace();
ex = ex.getNextException();
}
}catch(Exception e){
System.out.println("其他未知异常!");
e.printStackTrace();
}
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
不管哪种数据库用java连都是用jdbc,jdbc定义的都是接口,而这些实现是由数据库厂家来实现的,所以连接的方式都是一样,只是你连那种数据库,就用那个数据库的jdbc连接jar包。
具体:
1:Class.forName("连接字符串");
2:Connection c = DriverManager.getConnection();
3:Statement stmt = c.createStatemate();
4:ResultSet rs = stmt.execute("sql语句");
大体就这样
具体:
1:Class.forName("连接字符串");
2:Connection c = DriverManager.getConnection();
3:Statement stmt = c.createStatemate();
4:ResultSet rs = stmt.execute("sql语句");
大体就这样
追问
能把每个步骤的作用原理说一下吗?详细些!
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
现在一般使用jdbc啊 或者用hibernate封装好的就更好了
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询