坐穩扶好,老司機發車 了,
首先 在mysql裡邊建立一個資料庫,名字叫做jdbc。
然後在jdbc中建立一個叫person的表,
在表中添加元素
資料庫和表已經建好,然後串連,
建立一個工程,右鍵工程名建立一個檔案夾叫lib,然後把資料庫驅動拷貝進去,
樓主是好人,資料庫驅動在這:http://pan.baidu.com/s/1o8Qwco6
右鍵所添加的驅動,bulid bath,然後add bath,就build後出現的第一個,然後資料庫驅動就弄好啦:
剩下的 就是代碼部分:
package com.it.test;import java.sql.Connection;import java.sql.Driver;import java.sql.ResultSet;import java.sql.SQLException;import java.util.Properties;import java.sql.*;import com.mysql.jdbc.PreparedStatement;public class connection {static String url=("jdbc:mysql://localhost:3306/jdbc");//jdbc協議:資料庫子協議://主機:連接埠/要連結的資料庫static String user = "root";//使用者名稱static String password = "zhao123456";//密碼,請寫自己的密碼@SuppressWarnings("null")public static Connection test() throws SQLException{Driver drive=new com.mysql.jdbc.Driver();//建立驅動程式對象Properties pro = new Properties();//設定使用者名稱和密碼pro.setProperty("user", user);//賬戶pro.setProperty("password", password);//密碼Connection con = drive.connect(url, pro);//串連資料庫,返回對象System.out.println(con);//測試 連結是否成功return con;}public static void main(String[] args) throws SQLException {// TODO Auto-generated method stubConnection con=test();//串連資料庫 並測試String sql ="select * from person";//sql語句,查詢表PreparedStatement pstmt=null;//建立statementResultSet rs=null;pstmt=(PreparedStatement) con.prepareStatement(sql);rs = pstmt.executeQuery();//rs為查詢到的對象//System.out.println(rs);while(rs.next()){System.out.println(rs.getString("name")+" "+rs.getString("sex")+" "+rs.getString("age"));}System.out.println("-------更新-----------");String sql1="UPDATE person SET sex='女' WHERE age =18";String sex="女";System.out.println("1");//String age = "18";//pstmt.setString(1,sex);//pstmt.setInt(1, 18);//pstmt.setString(1, "");System.out.println("1");//pstmt.setString(2, age);pstmt=(PreparedStatement) con.prepareStatement(sql1);//先裝載更新語句pstmt.executeUpdate();//更新String sql2 ="select * from person";pstmt=(PreparedStatement) con.prepareStatement(sql2);//裝載查詢語句ResultSet rs1=pstmt.executeQuery();while(rs1.next()){System.out.println(rs1.getString("name")+" "+rs1.getString("sex")+" "+rs1.getString("age"));}System.out.println("-------添加-----------");PreparedStatement pstmt1=null;String sql3="INSERT INTO person VALUES ('趙六','男',19)";pstmt1=(PreparedStatement) con.prepareStatement(sql3);//裝載添加語句pstmt1.executeUpdate();//執行String sql6="select * from person";pstmt1=(PreparedStatement) con.prepareStatement(sql6);//裝載查詢語句//pstmt1.executeUpdate();//執行,,查詢語句不能更新。。。。。ResultSet rs2=pstmt1.executeQuery();//獲得while(rs2.next()){System.out.println(rs2.getString("name")+" "+rs2.getString("sex")+" "+rs2.getString("age"));}System.out.println("-------刪除-----------");String sql4="DELETE FROM person WHERE age=19";pstmt1=(PreparedStatement) con.prepareStatement(sql4);//裝載刪除語句pstmt1.executeUpdate();//執行String sql5 ="select * from person";pstmt1=(PreparedStatement) con.prepareStatement(sql5);//裝載查詢語句//pstmt1.executeUpdate();//執行ResultSet rs3=pstmt1.executeQuery();//獲得while(rs3.next()){System.out.println(rs3.getString("name")+" "+rs3.getString("sex")+" "+rs3.getString("age"));}pstmt.close();//關閉執行sql語句pstmt1.close();con.close();//關閉資料庫的串連}}
執行結果為:
最後強調一下,查詢語句,沒有這一句 //pstmt1.executeUpdate();//更新語句,,查詢語句不能更新。。。。。
如果添加了這一句,會出現這個異常:
Exception in thread "main" java.sql.SQLException: Can not issue executeUpdate() for SELECTs
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1055)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:956)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:926)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2004)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1964)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1949)
at com.it.test.connection.main(connection.java:79)
查詢語句沒有更新。。。。