JDBC的使用-----Statement

Posted QM.C

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了JDBC的使用-----Statement相关的知识,希望对你有一定的参考价值。

JDBC的查询步骤
1.加载数据库驱动类 1)在工程下新建lib文件夹,将 ojdbc6.jar(jar包在:E:oracleproduct11.2.0dbhome_1jdbclib)拷贝至该目录下 2)右键ojdbc6.jar文件,build path --> add too build path 3)将驱动类加载到内存中 Class.forName("oracle.jdbc.driver.OracleDriver"); 2.获取数据库连接对象 Connection conn = DriverManager.getConnection(url,user,password); 3.写sql String sql = "xxxxxx"; 4.创建PreparedStatement对象 PreparedStatement ps = conn.prepareStatement(sql); 5.执行查询返回ResultSet结果集对象 ResultSet rs = ps.executeQuery(); 6.遍历结果集,获取查询结果 while(rs.next){ rs.getXxx() } 7.释放资源 rs.close() ps.close() conn.close()
JDBC连接字符串
mysql: String Driver="com.mysql.jdbc.Driver"; //驱动程序 String URL="jdbc:mysql://localhost:3306/db_name"; //连接的URL,db_name为数据库名 String Username="username"; //用户名 String Password="password"; //密码 Class.forName(Driver); Connection con=DriverManager.getConnection(URL,Username,Password); Oracle: String Driver="oracle.jdbc.driver.OracleDriver"; //连接数据库的方法 String URL="jdbc:oracle:thin:@localhost:1521:orcl"; //orcl为数据库的SID String Username="username"; //用户名 String Password="password"; //密码 Class.forName(Driver) ; //加载数据库驱动 Connection con=DriverManager.getConnection(URL,Username,Password); //常用数据库链接方式 DB2: String Driver="com.ibm.dbjdbc.app.DBDriver"; //连接具有DB2客户端的Provider实例 //String Driver="com.ibm.dbjdbc.net.DBDriver"; //连接不具有DB2客户端的Provider实例 String URL="jdbc:db2://localhost:5000/db_name"; //db_name为数据库名 String Username="username"; //用户名 String Password="password"; //密码 Class.forName(Driver) ;

import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement;
public class TestStatement { public static void main(String[] args) { try { //1.加载数据库驱动文件 Class.forName("oracle.jdbc.driver.OracleDriver"); //2。获取数据库连接对象 String url = "jdbc:oracle:thin:@127.0.0.1:1521:orcl"; String user ="SCOTT"; String password = "tiger"; Connection conn = DriverManager.getConnection(url, user, password); //3.通过connection对象获取一个Statement对象,用于sql语句发送到数据库 testExecuteQuery(conn.createStatement()); System.out.println("--------------------"); testExecuteUpdate(conn.createStatement()); System.out.println("---------------------"); testExcute(conn.createStatement()); //5.遍历结果集 } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); System.out.println("请检查连接驱动类是否正确"); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public static void testExecuteUpdate(Statement stmt) throws SQLException { String sql = "insert into emp(empno,ename,sal) values(434,‘mm‘,5000)"; int result = stmt.executeUpdate(sql); System.out.println(result); } public static void testExecuteQuery(Statement stmt) throws SQLException { //4.获取结果集对象 //executeQuery 只能用在select关键字的查询中 String sql = "select e.ename ee,e.empno from emp e"; ResultSet rs = stmt.executeQuery(sql); while(rs.next()) { System.out.println(rs.getString("ee")+" "+rs.getInt("empno")); } } public static void testExcute(Statement stmt) throws SQLException { //execute方法可以执行任意的sql语句 String sql = "select * from emp"; boolean flag = stmt.execute(sql); ResultSet rs = stmt.getResultSet(); System.out.println(flag); while(rs.next()) { System.out.println(rs.getString("empno")); } } }

 

import java.sql.Connection;import java.sql.DatabaseMetaData;import java.sql.DriverManager;import java.sql.ResultSet;import java.sql.SQLException;
public class TestJDBCgetTable {public static void main(String[] args) {Connection conn = null;ResultSet columns = null;try {//加载数据库驱动文件Class.forName("oracle.jdbc.driver.OracleDriver");conn = DriverManager.getConnection("jdbc:oracle:thin:@127.0.0.1:1521:orcl", "SCOTT", "tiger");//获取数据库的元信息DatabaseMetaData metaData = conn.getMetaData();//查看数据库中有哪些表/*ResultSet rs = metaData.getTables(conn.getCatalog(), "SCOTT", null, new String[]{"TABLE"});while(rs.next()) {System.out.println(rs.getString(3));}*/columns = metaData.getColumns(conn.getCatalog(), "SCOTT", "EMP", null);while(columns.next()) {System.out.println(columns.getString(4));}} catch (ClassNotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}finally {try {if(columns!=null) {columns.close();}} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}try {if(conn!=null) {conn.close();}} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}}

 

以上是关于JDBC的使用-----Statement的主要内容,如果未能解决你的问题,请参考以下文章

Mysql JDBC代码实例

JDBC Statement对象执行批量处理实例

使用JDBC进行批处理

JDBC深入理解Statement和PreparedStatement

JDBC简介

JDBC的使用-----Statement