Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1109675
  • 博文数量: 284
  • 博客积分: 8223
  • 博客等级: 中将
  • 技术积分: 3188
  • 用 户 组: 普通用户
  • 注册时间: 2008-12-01 13:26
文章分类

全部博文(284)

文章存档

2012年(18)

2011年(33)

2010年(83)

2009年(147)

2008年(3)

分类: Java

2010-09-02 13:41:22

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


阅读(1651) | 评论(0) | 转发(0) |
0

上一篇:破解公司安全限制

下一篇:jni

给主人留下些什么吧!~~