java连接mysql数据库实例

做游戏客户端多一年多了,在大学学的java的SSH,基本上都忘完了,今天看了一下发现基本的连接数据库的都忘了。。。太可怕了这遗忘的速度。

所以写了个连接的例子吧。。安装好mysql数据库之后新建了两张表tx1,tx2。接下来连接数据库,往前面两张表里插入数据。

首先是公共连接类:

TestConnection.java

package cn.wan;

import java.sql.Connection;
import java.sql.*;

public class TestConnection {
      
    private   String driver;
    private  String url;
    private  String dbName;
    private  String password;
    Connection conn;
    Statement  sta;
    PreparedStatement prepare;
    
    public TestConnection()
    {
        this.driver = "com.mysql.jdbc.Driver";
        this.url = "jdbc:mysql://localhost:3306/tx";
        this.dbName = "root";
        this.password = "126";
    }
    
    public Connection getConnection()throws Exception
    {
        try {
            Class.forName(driver);
            conn = DriverManager.getConnection(url, dbName, password);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return conn;
    }
    
    public void closeConn()
    {
        try {
            if(this.conn!=null)
            {
                this.conn.close();
            }
        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
        }
    }
}

使用Statement类向tx1中插入数据,值得注意的是tx1表的key是整型的,所以注意插入数据的写法。。

package cn.wan;

import java.sql.Connection;
import java.sql.Statement;

public class TestStatement {
     
//    private static  TestConnection connection;
    
    public static void main(String[] args)throws Exception
    {
        Connection conn;
        Statement state = null;
        Long start = System.currentTimeMillis();
        TestConnection connection =new TestConnection();
        try {
            conn = connection.getConnection();
            state = conn.createStatement();
            // 需要使用100条sql语句来插入数据
            for(int i= 0;i<100;i++)
            {
                int num= i;
                String str2 = "name"+i;
                state.executeUpdate("insert into tx1 values('"+num+"','str"+i+"')");
            }
            System.out.println("使用Statement费时:"+(System.currentTimeMillis()- start));
        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
        }finally
        {
            connection.closeConn();
            if(state !=null)
            {
                state.close();
            }
        }
        
        
    }
}

使用PreparedStatement类向tx2表中插入数据,也要注意他的语句的写法:

package cn.wan;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.Statement;

public class TestPrepared {
   
    public static void main(String[] args)throws Exception
    {
        String sqlString = "insert into tx2 values(?,?)";
        Connection conn= null;
        PreparedStatement state = null;
        Long startLong = System.currentTimeMillis();
        TestConnection connection = new TestConnection();
        try {
            conn = connection.getConnection();
            // 使用Connection来创建一个PreparedStatemet对象
            state = conn.prepareStatement(sqlString);
            //  100次为PreparedStatemet的参数赋值,就能插入100条记录
            for(int i = 0;i<100;i++)
            {
                state.setInt(1, i);
                state.setString(2, i+"");
                state.executeUpdate();
            }
            System.out.println("使用PreparedStatemet耗时:"+(System.currentTimeMillis()-startLong));
        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
        }
        finally
        {
            connection.closeConn();
            if(state !=null)
            {
                state.close();
            }
        }
    }
}

至此连接和两种不同的数据插入就已经完成了。。

posted @ 2015-02-06 01:10  都市烟火  阅读(10425)  评论(0编辑  收藏  举报