文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>jdbc-mysql

jdbc-mysql

时间:2010-09-02  来源:cj_gameboy

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();
    }
}


相关阅读 更多 +
排行榜 更多 +
辰域智控app

辰域智控app

系统工具 下载
网医联盟app

网医联盟app

运动健身 下载
汇丰汇选App

汇丰汇选App

金融理财 下载