写一段Jdbc连接Oracle的程序,并实现数据查询。
创建一个连接数据库的工具类
import java.sql.;
public class DbUtil {
public static Connection getConnection(){
String driver = “”;
String url = “”;
String name = “scot”;
String psw = “123”;
Connection conn = null;
try {
Class.forName(driver);
conn = DriverManager.getConnection(url,name,psw);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
return conn;
}
}
import java.sql.;
public class SearchInfo {
public void searchInfo(int id){
Connection conn = null;
PreparedStatement pstat = null;
ResultSet res = null;
String sql = “select * from users where id=?”;
conn = DbUtil.getConnection();
try {
pstat = conn.prepareStatement(sql);
pstat.setInt(1, id);
res = pstat.executeQuery();
while(res.next()){
String name = res.getString(“name”);
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
写一段Jdbc连接Oracle的程序,并实现数据查询
最新推荐文章于 2021-09-11 11:24:09 发布