zoukankan      html  css  js  c++  java
  • 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();
                }
            }
        }
    }

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

  • 相关阅读:
    关于订单创建的service层
    使用注解@RestController返回json类型的数据
    关于lombok包(可使编程便捷)的一些使用
    Django学习笔记一十三——ORM查询练习
    Django学习笔记一十二——建立多对多结构表的三种方式
    Django学习笔记一十一——ORM学习三
    Django学习笔记一十——Django项目在python脚本中的调用
    Django学习笔记〇九——路由系统
    Django学习笔记〇八——模板语言系统
    Django学习笔记〇七——MCV和MTV框架介绍
  • 原文地址:https://www.cnblogs.com/duhuo/p/4276226.html
Copyright © 2011-2022 走看看