DROP TABLE IF EXISTS `user`; CREATE TABLE `user` ( `id` int(11) NOT NULL AUTO_INCREMENT, `name` varchar(255) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL DEFAULT '', `password` varchar(8) DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8; -- ---------------------------- -- Records of user -- ---------------------------- INSERT INTO `user` VALUES ('1', 'liyuhang', '123321'); INSERT INTO `user` VALUES ('2', 'sunmiaomiao', '654321');
Connection:表示一個數據庫鏈接java
Statement:用來執行SQLmysql
PreparedStatement:用來執行可複用SQL,主要是能夠傳參web
CallableStatement:用來執行存儲過程sql
ResultSet:表示SQL執行的結果集數據庫
ResultSetMetaData:結果集的元數據,用來獲取結果集的相關信息json
直接上代碼測試
package json712.study_jdbc; import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; public class JdbcUtils { private static final String url = "jdbc:mysql://192.168.1.103:3306/study_web?useSSL=false"; private static final String user = "json"; private static final String password = "123456"; static{ try { Class.forName("com.mysql.jdbc.Driver"); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public static Connection getConnection(){ try { Connection connection=DriverManager.getConnection(url, user, password); return connection; } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } return null; } };
@Test public void queryTest(){ Connection connection=JdbcUtils.getConnection(); try { Statement statement=connection.createStatement(); String sql="select * from user "; ResultSet resultSet=statement.executeQuery(sql); while (resultSet.next()) { int id=resultSet.getInt(1); String name=resultSet.getString(2); System.out.println("id:"+id+" name:"+name); } statement.close(); } catch (SQLException e) { e.printStackTrace(); } try { connection.close(); } catch (SQLException e) { e.printStackTrace(); } }
運行結果:url
id:1 name:liyuhang id:2 name:sunmiaomiao
@Test public void prepareStatementTest(){ Connection connection=JdbcUtils.getConnection(); String sql=" select * from user where id=?"; try { PreparedStatement statement=connection.prepareStatement(sql); statement.setInt(1, 1); ResultSet resultSet=statement.executeQuery(); while (resultSet.next()) { int id=resultSet.getInt(1); String name=resultSet.getString(2); String password=resultSet.getString(3); System.out.println("id:"+id+" name:"+name+" password:"+password); } statement.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } try { connection.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
運行結果:code
id:1 name:liyuhang password:123321
@Test public void inserBatchTest(){ Connection connection=JdbcUtils.getConnection(); try { PreparedStatement statement=connection.prepareStatement("INSERT INTO `user`\n" + "(`name`,`PASSWORD`)\n" + "VALUES\n" + " (?, ?)"); String[] name=new String[]{"wt","gaojl"}; String[] password=new String[]{"031512","222222"}; for (int i = 0; i < password.length; i++) { statement.setString(1, name[i]); statement.setString(2, password[i]); statement.addBatch(); } statement.executeBatch(); } catch (SQLException e) { e.printStackTrace(); } }
腳本準備get
CREATE PROCEDURE `hi`() SELECT 'hello'
測試代碼:
@Test public void callprocedureTest(){ Connection connection=JdbcUtils.getConnection(); try { CallableStatement prepareCall = connection.prepareCall("call hi()"); prepareCall.execute(); ResultSet resultSet=prepareCall.getResultSet(); while (resultSet.next()) { String hl=resultSet.getString(1); System.out.println(hl); } prepareCall.close(); connection.close(); } catch (SQLException e) { e.printStackTrace(); } }
運行結果:
hello
腳本準備
CREATE PROCEDURE `pr_add`( a int, b int ) begin declare c int; if a is null then set a = 0; end if; if b is null then set b = 0; end if; set c = a + b; select c as sum; end
測試代碼:
@Test public void callprocedureInTest(){ Connection connection=JdbcUtils.getConnection(); try { CallableStatement statement =connection.prepareCall("call pr_add(?,?)"); statement.setInt(1, 10); statement.setInt(2, 20); statement.execute(); ResultSet resultSet=statement.getResultSet(); while (resultSet.next()) { System.out.println(resultSet.getInt(1)); } statement.close(); connection.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
運行結果:
30
腳本準備:
CREATE PROCEDURE `testoutput`(a int,b int, out c int) BEGIN if a is null then set a=0; end if; if b is null THEN set b=0; end if; set c=a+b; end
測試代碼:
@Test public void callprocedureOutTest(){ Connection connection=JdbcUtils.getConnection(); try { CallableStatement statement =connection.prepareCall("call testoutput(?,?,?)"); statement.setInt(1, 10); statement.setInt(2, 20); statement.registerOutParameter(3, Types.INTEGER) ; statement.execute(); System.out.println(statement.getInt(3)); statement.close(); connection.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
運行結果:
30
測試代碼:
@Test public void metadataTest(){ Connection connection=JdbcUtils.getConnection(); try { Statement statement=connection.createStatement(); String sql="select * from user "; ResultSet resultSet=statement.executeQuery(sql); ResultSetMetaData metadata=resultSet.getMetaData(); for(int i=1;i<=metadata.getColumnCount();i++){ String label=metadata.getColumnLabel(i); int size=metadata.getColumnDisplaySize(i); String columnname=metadata.getColumnName(i); String tablename=metadata.getTableName(i); String classname=metadata.getColumnClassName(i); //metadata. //System.out.println("cname:"+columnname+" classname:"+classname); System.out.println("label:"+label+" displaysize:"+size+" columnname:"+columnname+" tablename:"+tablename); } statement.close(); } catch (SQLException e) { e.printStackTrace(); } try { connection.close(); } catch (SQLException e) { e.printStackTrace(); } }
運行結果:
label:id displaysize:11 columnname:id tablename:user label:name displaysize:255 columnname:name tablename:user label:password displaysize:8 columnname:password tablename:user