02-JDBC连接MySQL数据库【查询数据】
生活随笔
收集整理的這篇文章主要介紹了
02-JDBC连接MySQL数据库【查询数据】
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
JDBC自學教程–終篇總結:
地址:http://blog.csdn.net/baidu_37107022/article/details/72600018
1.實現查詢步驟
1)實現注冊的兩種方式
2)得到Connection的三種方式
3)創建Statement的兩種方式
2.jdbc查詢實戰演練
這里使用的是queryDemo數據庫,表格為demo1student,表中數據如下:
1)查詢所有信息
前三步:注冊,Connection,Statement方法選擇如下: DriverManager.registerDriver(new Driver()); DriverManager.getConnection(String url); createStatement()代碼演示
import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement;import com.mysql.jdbc.Driver;public class Test {public static void main(String[] args) {Test test=new Test();test.firstJDBC();}/* 1.DriverManager:數據庫驅動:多個(每個數據庫對應一個驅動)2.Connection:得到數據庫連接3.得到數據庫的執行sql的對象Statement;4.執行sql語句5.得到結果;6.關閉連接資源*/public void firstJDBC(){Connection connection=null;Statement statement=null;ResultSet resultSet=null;try {//1.registerDriverManager.registerDriver(new Driver());//2.得到數據連接 url格式:jdbc:mysql://主機IP:端口號/數據庫名?user=用戶名&password=密碼 //因為MySQL安裝時,端口號默認設置的是3306,所以都是3306String url="jdbc:mysql://localhost:3306/queryDemo?user=root&password=123";connection=DriverManager.getConnection(url);//3.得到數據庫的執行sql對象statement=connection.createStatement();//4.執行語句String sql="select * from demo1student"; //SQL查詢語句resultSet=statement.executeQuery(sql);while(resultSet.next()){//取出查詢的信息String name=resultSet.getString("name");int age=resultSet.getInt("age");int score=resultSet.getInt("score");System.out.println("name="+name+",age="+age+",score="+score);}} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}finally {//5.關閉資源if(connection!=null){try {connection.close();} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}}if(statement!=null){try {statement.close();} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}}if(resultSet!=null){try {resultSet.close();} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}}}運行結果:
注意:只需要改變第四步中的sql語句,根據需求調整SQL語句即可
2)查詢部分信息
前三步:注冊,Connection,Statement方法選擇如下: Class.forName("com.mysql.jdbc.Driver"); DriverManager.getConnection(String url, Properties info); prepareStatement()代碼演示
import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.Properties;import org.junit.Test;public class Test6 {@Testpublic void Query1(){Connection connection=null;PreparedStatement ps=null;ResultSet resultSet=null;try {//1.registerClass.forName("com.mysql.jdbc.Driver");//2.getConnectionString url="jdbc:mysql://localhost:3306/queryDemo";Properties info=new Properties();info.put("user", "root");info.put("password", "123");connection=DriverManager.getConnection(url, info);//3.create StatementString sql="select * from demo1student where id=? or id=?";ps=connection.prepareStatement(sql);//sexInt() 參數1:sql語句中?所在的位置 參數2:給?的位置賦值ps.setInt(1, 2); //給第一個id賦值為2ps.setInt(2, 4); //給第二個id賦值為4//4.excuteQueryresultSet=ps.executeQuery();while(resultSet.next()){//取出查詢的信息String name=resultSet.getString("name");int age=resultSet.getInt("age");int score=resultSet.getInt("score");int id=resultSet.getInt("id");System.out.println("id="+id+",name="+name+",age="+age+",score="+score);}} catch (ClassNotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}finally {//5. close resourceif(connection!=null){try {connection.close();} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}}if(ps!=null){try {ps.close();} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}}if(resultSet!=null){try {resultSet.close();} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}} }運行結果:
《新程序員》:云原生和全面數字化實踐50位技術專家共同創作,文字、視頻、音頻交互閱讀總結
以上是生活随笔為你收集整理的02-JDBC连接MySQL数据库【查询数据】的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 01-JDBC概念--JDBC(Java
- 下一篇: 03-JDBC连接MySQL数据库【插入