java中嵌入sql語句
阿新 • • 發佈:2019-02-14
public static void main(String[] args)throws ClassNotFoundException,SQLException {
//4要素
String driverName = "oracle.jdbc.driver.OracleDriver";
driverName = "oracle.jdbc.OracleDriver";
String url = "jdbc:oracle:thin:@localhost:1521:orcl";
String username = "scott";
String password = "123";
//註冊驅動獲得連線
Class.forName(driverName);
Connection conn = DriverManager.getConnection(url,username,password);
//獲得語句物件
Statement stmt = conn.createStatement();
//傳送sql語句
String sql = "select empno,ename,sal from emp";
ResultSet rs = stmt.executeQuery(sql);
//解析結果
while(rs.next()){
String empno = rs.getString(1);
String ename = rs.getString("ename");
String sal = rs.getString(3);
System.out.println(empno+" "+ename+" "+sal);
}
rs.close();
stmt.close();
conn.close();
}
}
//4要素
String driverName = "oracle.jdbc.driver.OracleDriver";
driverName = "oracle.jdbc.OracleDriver";
String url = "jdbc:oracle:thin:@localhost:1521:orcl";
String username = "scott";
String password = "123";
//註冊驅動獲得連線
Class.forName(driverName);
Connection conn = DriverManager.getConnection(url,username,password);
//獲得語句物件
Statement stmt = conn.createStatement();
//傳送sql語句
String sql = "select empno,ename,sal from emp";
ResultSet rs = stmt.executeQuery(sql);
//解析結果
while(rs.next()){
String empno = rs.getString(1);
String ename = rs.getString("ename");
String sal = rs.getString(3);
System.out.println(empno+" "+ename+" "+sal);
}
rs.close();
stmt.close();
conn.close();
}
}