本文实例讲述了jdbc操作mysql数据库的方法。分享给大家供大家参考。具体如下:
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程序设计有所帮助。
标签:
jdbc,mysql数据库
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件!
如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
桃源资源网 Design By www.nqtax.com
暂无“jdbc操作mysql数据库实例”评论...