jdbc操作mysql數(shù)據(jù)庫實(shí)例
更新時(shí)間:2015年09月16日 11:37:12 作者:cj_gameboy
這篇文章主要介紹了jdbc操作mysql數(shù)據(jù)庫的方法,涉及jsp基于jdbc針對mysql數(shù)據(jù)庫的連接、插入、查詢等簡單操作技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下
本文實(shí)例講述了jdbc操作mysql數(shù)據(jù)庫的方法。分享給大家供大家參考。具體如下:
import java.sql.*; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; class conn{ String url="jdbc:mysql://127.0.0.1/nariData"; final String user = "nari"; final String pass = "nariacc"; Connection conn = null; Statement st = null; ResultSet rs = null; conn(){ try{ Class.forName("com.mysql.jdbc.Driver"); }catch(Exception e){ e.printStackTrace(); } try{ System.out.println("in DBManager"); conn = DriverManager.getConnection(url, user, pass); String sql = "create table test (id int, uid int)"; st = conn.createStatement(); st.execute(sql); }catch(Exception e){ e.printStackTrace(); } } void insert(){ try{ for(int i=0;i<10;i++){ String sql = "insert into test values("+i+","+i+")"; st.execute(sql); } }catch(Exception e){ e.printStackTrace(); } } void query(){ try{ String sql = "select * from test "; rs = st.executeQuery(sql); while(rs.next()){ System.out.print("res1: "+rs.getInt(1)+" res2: "+rs.getInt(2)+"\n"); } }catch(Exception e){ e.printStackTrace(); } } void close(){ try{ rs.close(); st.close(); conn.close(); }catch(Exception e){ e.printStackTrace(); } } } public class mysqltest { public static void main(String[] args) { // TODO Auto-generated method stub conn a1 = new conn(); a1.insert(); a1.query(); a1.close(); } }
希望本文所述對大家的JSP程序設(shè)計(jì)有所幫助。
您可能感興趣的文章:
- java jdbc連接mysql數(shù)據(jù)庫實(shí)現(xiàn)增刪改查操作
- JSP使用JDBC連接MYSQL數(shù)據(jù)庫的方法
- Java 通過JDBC連接Mysql數(shù)據(jù)庫
- JDBC對MySQL數(shù)據(jù)庫布爾字段的操作方法
- java使用jdbc連接數(shù)據(jù)庫工具類和jdbc連接mysql數(shù)據(jù)示例
- MyEclipse通過JDBC連接MySQL數(shù)據(jù)庫基本介紹
- Java使用JDBC連接數(shù)據(jù)庫的實(shí)現(xiàn)方法
- JDBC數(shù)據(jù)庫的使用操作總結(jié)
- JDBC連接Oracle數(shù)據(jù)庫常見問題及解決方法
- MySQL為例講解JDBC數(shù)據(jù)庫連接步驟
相關(guān)文章
JSP開發(fā)之hibernate之單向多對一關(guān)聯(lián)的實(shí)例
這篇文章主要介紹了JSP開發(fā)之hibernate之單向多對一關(guān)聯(lián)的實(shí)例的相關(guān)資料,希望通過本文能幫助到大家,讓大家實(shí)現(xiàn)這樣的功能,需要的朋友可以參考下2017-09-09JSP內(nèi)置對象:Request和Response的簡單介紹及使用
JSP內(nèi)置對象:Request和Response的簡單介紹及使用,需要的朋友可以參考一下2013-02-02Linux和Windows中tomcat修改內(nèi)存大小的方法
Linux和Windows中tomcat修改內(nèi)存大小的方法,可以利用JVM提供的-Xmn -Xms -Xmx等選項(xiàng)可進(jìn)行設(shè)置,大家參考使用吧2013-12-12JSP實(shí)現(xiàn)簡單的用戶登錄并顯示出用戶信息的方法
這篇文章主要介紹了JSP實(shí)現(xiàn)簡單的用戶登錄并顯示出用戶信息的方法,通過簡單的登陸及登陸顯示頁面實(shí)現(xiàn)這一功能,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-02-02