package com;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class Text {
//增加驱动
public static void main(String[] args) {
//2.注册驱动
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
//3.建立连接
String str = "jdbc:oracle:thin:@192.168.31.21:1521:orcl";
Connection con = DriverManager.getConnection(str, "scott", "tiger");
System.out.println("*************正在连接数据库***********");
//4.创建语句
Statement stmt = con.createStatement();
//5.执行语句
ResultSet rs = stmt.executeQuery("select empno,SAL,COMM*2 from emp ");
//6.处理结果
System.out.println("*************已连接连接数据库*********");
System.out.println("*************数据库查询结果**********");
while(rs.next())
{
int i = rs.getInt(1);
int a = rs.getInt(2);
int b = rs.getInt(3);
if(rs.wasNull())
{
System.out.println(i+"\t"+a+"\t"+"无");
}
else
{
String query2 = "update emp set comm=comm*2 where empno="+i;
Statement stmtt = con.createStatement();
stmtt.executeUpdate(query2);
System.out.println(i+"\t"+a+"\t"+b);
}
}
//7.释放资源
rs.close();
stmt.close();
con.close();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
if(e.getErrorCode()==1017)//getErrorCode检索此 SQLException 对象的特定于供应商的异常代码
System.out.println("用户名或密码错误");
else
{
System.out.println(e.getMessage());//getMessage 方法用来获取异常的信息
}
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}