有时候localhost 改写成 本地ip oracle 性能强大,功能卓越
ojdbc14.jar
package demo1;
/**
* JDBC绀轰緥1
*/
import java.sql.*;
/**
* @author Administrator
* 锟津单碉拷JDBC示锟斤拷
*/
public class JDBCDemo1 {
/**
* @param args
*/
public static void main(String[] args) {
String driver = "oracle.jdbc.driver.OracleDriver";
String database = "CHENMING";
String url = "jdbc:oracle:thin:@localhost:1521:CHENMING";
String userName = "SCOTT";
String password = "tiger";
Connection con = null;
Statement st = null;
ResultSet rs = null;
String sql = null;
try {
Class.forName(driver);
System.out.println(url);
System.out.println(userName);
System.out.println(password);
con = DriverManager.getConnection(url,userName,password);
System.out.println("Database connected successfully.");
st = con.createStatement();
sql = "SELECT * FROM EMP";
rs = st.executeQuery(sql);
while(rs.next()){
System.out.println("---------------------------------------------");
System.out.print("EMPNO:" + rs.getInt("EMPNO") + "\t");
System.out.print("ENAME:" + rs.getString("ENAME") + "\t");
System.out.print("JOB:" + rs.getString("JOB") + "\t");
System.out.print("MGR:" + rs.getInt("MGR") + "\t");
System.out.print("HIREDATE:" + rs.getDate("HIREDATE") + "\t");
System.out.print("SAL:" + rs.getInt("SAL") + "\t");
System.out.print("COMM:" + rs.getInt("COMM") + "\t");
System.out.println("DEPTNO:" + rs.getInt("DEPTNO"));
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} finally{
if(rs != null){
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(st != null){
try {
st.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(con != null){
try {
con.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
}
package com.ttc.test.dao;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import com.ttc.test.vo.UserInfo;
public class QueryDao {
public UserInfo queryByName(String name){
//首先導入jar
//加載JDBC驅動
//建立于數據庫的聯繫
//创建statement
//执行sql,于数据库交互
//处理结果
//关闭连接
String driver = "oracle.jdbc.driver.OracleDriver";
String database = "orcl";
String url = "jdbc:oracle:thin:@10.10.21.145:1521:" + database;
String userName = "SCOTT";
String password = "neufort";
Connection con = null;
//预编译
PreparedStatement pStatement = null;
ResultSet rs = null;
String sql = null;
UserInfo userInfo = new UserInfo();
try {
Class.forName(driver);
con = DriverManager.getConnection("jdbc:oracle:thin:@10.10.21.145:1521:orcl","scott","neusoft");
System.out.println("Database connected successfully.");
pStatement = con.prepareStatement("select * from demo_users where username=?");
pStatement.setString(1,name);
rs = pStatement.executeQuery();
while(rs.next()){
System.out.println("query success");
userInfo.setUserName(rs.getString(1));
userInfo.setPassWord(rs.getString(2));
userInfo.setName(rs.getString(3));
userInfo.setBirthDate(rs.getString(4));
userInfo.setSex(rs.getString(5));
userInfo.setInterest(rs.getString(6));
userInfo.setDegree(rs.getString(7));
userInfo.setIntro(rs.getString(8));
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} finally{
//防止空指针异常
if(rs != null){
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(pStatement != null){
try {
pStatement.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(con != null){
try {
con.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
return userInfo;
}
}