MySql 執行JDBC聯結(增/刪/改/查)操作,mysqljdbc

來源:互聯網
上載者:User

MySql 執行JDBC聯結(增/刪/改/查)操作,mysqljdbc

視頻地址:http://www.tudou.com/programs/view/4GIENz1qdp0/


建立BaseDao

package cn.wingfly.dao;import java.sql.Connection;import java.sql.DriverManager;import java.sql.ResultSet;import java.sql.SQLException;import java.sql.Statement;public class BaseDao {Connection con = null;Statement st = null;ResultSet rs = null;/** * 獲得聯結 *  * @return */public Connection getConnection() {try {// 載入驅動,這一句也可寫為:Class.forName("com.mysql.jdbc.Driver"); Class.forName("com.mysql.jdbc.Driver").newInstance();// 建立到MySQL的串連 con = DriverManager.getConnection("jdbc:mysql://localhost:3306/money_note?characterEncoding=UTF-8", "root", "root");} catch (Exception e) {e.printStackTrace();}return con;}/** * 關閉資料來源 */public void CloseConnection(Connection con, Statement s, ResultSet rs) {try {if (rs != null) {rs.close();}if (s != null) {s.close();}if (con != null) {con.close();}} catch (SQLException e) {e.printStackTrace();}}}

測試類別

package cn.wingfly.dao;import java.sql.Connection;import java.sql.ResultSet;import java.sql.SQLException;import java.sql.Statement;public class Test extends BaseDao {Connection con = null;Statement st = null;ResultSet rs = null;/** * 查詢資料 */public void find() {con = getConnection();// 獲得聯結try {st = con.createStatement();rs = st.executeQuery("select * from app_user");while (rs.next()) {System.out.println("編號:" + rs.getInt("uuid") + ", 姓名:" + rs.getString("userName") + ", 性別:" + rs.getString("sex") + ", 生日:" + rs.getString("birthday") + ", 住址:" + rs.getString("address"));}} catch (SQLException e) {e.printStackTrace();} finally {CloseConnection(con, st, rs);// 關閉聯結}}/** * 添加資料 */public void add() {con = getConnection();try {st = con.createStatement();int result = st.executeUpdate("insert into app_user(userName,passWord,sex,birthday,address) values('趙麗穎','wanying','女','1992-02-03','北京市')");if (result > 0) {System.out.println("插入成功");} else {System.out.println("插入失敗");}} catch (SQLException e) {System.out.println("插入失敗");e.printStackTrace();} finally {CloseConnection(con, st, rs);}}/** * 更新資料 */public void update() {con = getConnection();try {st = con.createStatement();int result = st.executeUpdate("update app_user set address = '河南' where uuid = '3'");if (result > 0) {System.out.println("更新成功");} else {System.out.println("更新失敗");}} catch (SQLException e) {e.printStackTrace();} finally {CloseConnection(con, st, rs);}}/** * 刪除資料 */public void delete() {con = getConnection();try {st = con.createStatement();int result = st.executeUpdate("delete from app_user where uuid = '3'");if (result > 0) {System.out.println("刪除成功");} else {System.out.println("刪除失敗");}} catch (SQLException e) {e.printStackTrace();} finally {CloseConnection(con, st, rs);}}public static void main(String[] args) {Test test = new Test();//test.add();// test.update(); test.delete();test.find();}}



java中的jdbc 怎實現mysql 增刪改查? jdbc串連mysql有是缺點? mysql確定不收費?

/**
* 尋找資料庫總共有多少條記錄
*/
public int findAllEmailCount() {
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
String sql = "select count(*) from email";
int count = 0;
try {
conn = DBHelp.getConnection();
pstmt = conn.prepareStatement(sql);
rs = pstmt.executeQuery();
if(rs.next()){
count = rs.getInt(1);
}
} catch (Exception e) {
throw new RuntimeException("尋找資料庫出錯");
} finally {
DBHelp.closeAll(null, pstmt, conn);
}
return count;
}
 
編寫一個java程式,通過jdbc訪問資料庫實現對資料庫的插入,刪除,更改與查詢操作

1.增加
String s1="insert into tableNames (id,name,password) values(myseq.nextval,?,?);"
Class.forName(driver);
Connection conn = DriverManager.getConnection(url,dbUser,dbPwd);
PreparedStatement prepStmt = conn.prepareStatement(s1);
prepStmt.setString(1,name);
prepStmt.setString(2,password);
ResultSet rs=stmt.executeUpdate();
2、刪除
String s2="delete from tbNames where name=?";
Class.forName(driver);
Connection conn = DriverManager.getConnection(url,dbUser,dbPwd);
PreparedStatement prepStmt = conn.prepareStatement(s2);
prepStmt.setString(1,name);
ResultSet rs=stmt.executeUpdate();
3、修改
String s3=“update tbNames set name=? where id=?”;
Class.forName(driver);
Connection conn = DriverManager.getConnection(url,dbUser,dbPwd);
PreparedStatement prepStmt = conn.prepareStatement(s3);
prepStmt.setString(1,name);
prepStmt.setString(2,id);
ResultSet rs=stmt.executeUpdate();
4、查詢
String s4="select id,name,password from tbNames";
Class.forName(driver);
Connection conn = DriverManager.getConnection(url,dbUser,dbPwd);
Statement stmt=conn.createStatement();
ResultSet rs = stmt.executeQuery(s4);
while(rs.next){
int id=rs.getInt(1);
String name = rs.getString(2);
String pwd=rs.getString(3);
System.out.println(id+name+pwd); }

以上四步必須都得關閉串連;!!!
rs.close();
stmt.close();
conn.close();...餘下全文>>
 

相關文章

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.