代码语言:javascript复制
public class LangDemo {
public static void main(String[] args) throws Exception{
try {
//加载驱动
Class.forName("oracle.jdbc.driver.OracleDriver");
//创建连接
Connection cn=DriverManager.getConnection("jdbc:oracle:thin:@127.0.0.1:1521:XE","hh","hh");
Statement sm=cn.createStatement();
//获得结果集
ResultSet rs=sm.executeQuery("select name,nickname,id from a");
//遍历结果集
while(rs.next()){
System.out.println("name:" rs.getString(1));//rs.getString(“name”)
System.out.println("nickname:" rs.getString(2)); //rs.getInt(“id”)
}
} catch (Exception e) {
e.printStackTrace();
}finally{ // rs.close();
// conn.close();
}
}
}
代码语言:javascript复制public class LangDemo {
public static void main(String[] args) throws Exception{
Connection con = null;// 创建一个数据库连接
PreparedStatement pre = null;// 创建预编译语句对象,一般都是用这个而不用Statement
ResultSet result = null;// 创建一个结果集对象
try
{
Class.forName("oracle.jdbc.driver.OracleDriver");// 加载Oracle驱动程序
System.out.println("开始尝试连接数据库!");
String url = "jdbc:oracle:" "thin:@127.0.0.1:1521:XE"; // 127.0.0.1是本机地址,XE是精简版Oracle的默认数据库名
String user ="system";// 用户名,系统默认的账户名
String password = "147";// 你安装时选设置的密码
con = DriverManager.getConnection(url, user, password);// 获取连接
System.out.println("连接成功!");
String sql = "select * from student where name=?";// 预编译语句,“?”代表参数
pre = con.prepareStatement(sql);// 实例化预编译语句
pre.setString(1, "张三");// 设置参数,前面的1表示参数的索引,而不是表中列名的索引
result = pre.executeQuery();// 执行查询,注意括号中不需要再加参数
while (result.next())
// 当结果集不为空时
System.out.println("学号:" result.getInt("id") "姓名:"
result.getString("name"));
}
catch (Exception e)
{
e.printStackTrace();
}
finally {
……..
}
}
}
jdbc也有事物:connection.setAutoCommit(false) 默认是true自动提交
connection.commit();
connection.rollback();
代码语言:javascript复制public class LangDemo {
public static void main(String[] args) throws Exception{
try {
//加载驱动
Class.forName("oracle.jdbc.driver.OracleDriver");
//创建连接
Connection cn=DriverManager.getConnection("jdbc:oracle:thin:@127.0.0.1:1521:XE","hh","hh");
//调用存储过程(存储过程的两个值都是in)
CallableStatement sm=cn.prepareCall("{call lv_three(?,?)}");
//赋值
sm.setString(1, "jerry");
sm.setInt(2, 1800);
sm.execute();
//释放资源
sm.close();
cn.close();
} catch (Exception e) {
e.printStackTrace();
}finally{
// sm.close();
// conn.close();
}
}
}
代码语言:javascript复制public class LangDemo {
public static void main(String[] args) throws Exception{
try {
//加载驱动
Class.forName("oracle.jdbc.driver.OracleDriver");
//创建连接
Connection cn=DriverManager.getConnection("jdbc:oracle:thin:@127.0.0.1:1521:XE","hh","hh");
//调用存储过程(存储过程的第一个参数是in,第二个参数是out)
CallableStatement sm=cn.prepareCall("{call lv_four(?,?)}");
//赋值
sm.setInt(1, 1);
sm.registerOutParameter(2, oracle.jdbc.OracleTypes.VARCHAR);//固定的
sm.execute() String namestr=sm.getString(2);
System.out.println("名称是:" namestr);
sm.close(); //释放资源
cn.close();
} catch (Exception e) {
e.printStackTrace();
}finally{
// sm.close();
// conn.close();
}
}
}
代码语言:javascript复制public class LangDemo {
public static void main(String[] args) throws Exception{
try {
//加载驱动
Class.forName("oracle.jdbc.driver.OracleDriver");
//创建连接
Connection cn=DriverManager.getConnection("jdbc:oracle:thin:@127.0.0.1:1521:XE","hh","hh");
//调用存储过程(两个out参数)
CallableStatement sm=cn.prepareCall("{call lv_four(?,?)}");
//赋值
sm.registerOutParameter(1, oracle.jdbc.OracleTypes.NUMBER);
sm.registerOutParameter(2, oracle.jdbc.OracleTypes.VARCHAR);
sm.execute();
int sid=sm.getInt(1);
String namestr=sm.getString(2);
System.out.println(“名称是:” namestr ”id是” sid);
sm.close(); //释放资源
cn.close();
} catch (Exception e) {
e.printStackTrace();
}finally{
// sm.close();
// conn.close();
}
}
}
代码语言:javascript复制public class LangDemo {
public static void main(String[] args) throws Exception{
try {
//加载驱动
Class.forName("oracle.jdbc.driver.OracleDriver");
//创建连接
Connection cn=DriverManager.getConnection("jdbc:oracle:thin:@127.0.0.1:1521:XE","hh","hh");
//调用存储过程(参数为in out类型)
CallableStatement sm=cn.prepareCall("{call getsal(?)}");
//赋值
sm.setInt(1,1); // sm.setDouble(1,800);
sm.registerOutParameter(1, oracle.jdbc.OracleTypes.NUMBER);
sm.execute();
int sid=sm.getInt(1); // double d = sm.getDouble(1)
System.out.println("id是:" sid);
sm.close(); //释放资源
cn.close();
} catch (Exception e) {
e.printStackTrace();
}finally{
// sm.close();
// conn.close();
}
}
}
如果是连接mysql
加载驱动:
代码语言:javascript复制 Class.forName("com.mysql.jdbc.Driver");
String url="jdbc:mysql://localhost:3306/db_pms"; //注意:db_pms是自定义数据库的名字