/**
*
*/
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.ResultSet;
import java.sql.Statement;
import java.sql.DatabaseMetaData;
/**
* @author kuzuozhou *
*/
public class Connect {
/**
* @param args
*/
//伺服器地址:127.0.0.1,埠號:1521,資料庫例項名字:orcl123。
private String url="jdbc:oracle:thin:@127.0.0.1:1521:orcl123";
//使用者名稱字和密碼是自己建立的
private String username="system";
private String pw="fupeng";
private String sqlInsert = "INSERT INTO student(Sid,Sname) VALUES(1005,'chen7')";
private Connection conn=null;
private Statement stmt = null;
private ResultSet rs = null;
public Connection Conn1(){
try{
Class.forName("oracle.jdbc.driver.OracleDriver");
try{
conn = DriverManager.getConnection(url, username, pw);
//added by fupeng
// DatabaseMetaData dmd = conn.getMetaData();//當前資料庫的資訊
// System.out.println(dmd.getDatabaseProductName());
// System.out.println(dmd.getDatabaseProductVersion());
// System.out.println(dmd.getDriverName());
// System.out.println(dmd.getURL());
// System.out.println(dmd.getUserName());
stmt = conn.createStatement();
// rs = stmt.executeQuery("select *from student");
// System.out.println(sqlInsert);
int i = stmt.executeUpdate(sqlInsert);
// while(rs.next()){
//// System.out.println("hello!!!");
//// String s = rs.getString("Sname");
//// System.out.println(s.toLowerCase());
// System.out.println(rs.getString("Sid")+" "+rs.getString("Sname"));
// }
//釋放資源
rs.close();
stmt.close();
conn.close();
// String s = rs.getString("SID");
// System.out.println(s.toLowerCase());
}catch(SQLException e){
e.printStackTrace();
}
}catch (ClassNotFoundException e){
e.printStackTrace();
}
return conn;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Connect obj = new Connect();
Connection con = obj.Conn1();
if(con == null){
System.out.println("連線失敗");
}
else{
System.out.println("連線成功");
System.out.println(con);
}
}
}