jdbc操作mysql数据库实例
程序员文章站
2023-12-24 18:00:09
本文实例讲述了jdbc操作mysql数据库的方法。分享给大家供大家参考。具体如下:
import java.sql.*;
import java.sql.dri...
本文实例讲述了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程序设计有所帮助。