跳至主要内容

java jdbc mysql

1. 连接数据库
  (1) 下载Mysql连接驱动
网址: http://dev.mysql.com/downloads/connector/j/ ,下载后放在F:\博士科研资料\数据库学习\mysql相关程序文件中,解压。
  (2) 加载JDBC驱动
操作方法:在Eclipse中,选中相应的工程,点击Project-Properties中的Java Build Path,在Libraries中增加mysql-connector-java-5.1.21-bin.jar,点OK。
  (3) 建一个简单的数据库如下:
                  
复制代码
import java.sql.*;
public class GetConnection {
    public static void main(String[] args){
        try{
            //调用Class.forName()方法加载驱动程序
            Class.forName("com.mysql.jdbc.Driver");
            System.out.println("成功加载MySQL驱动!");
        }catch(ClassNotFoundException e1){
            System.out.println("找不到MySQL驱动!");
            e1.printStackTrace();
        }
        
        String url="jdbc:mysql://localhost:3306/mysql";    //JDBC的URL    
        //调用DriverManager对象的getConnection()方法,获得一个Connection对象
        Connection conn;
        try {
            conn = DriverManager.getConnection(url,    "root","");
            //创建一个Statement对象
            Statement stmt = conn.createStatement(); //创建Statement对象
            System.out.print("成功连接到数据库!");
            stmt.close();
            conn.close();
        } catch (SQLException e){
            e.printStackTrace();
        }
    }
}
复制代码

2. 查询数据表
  在询数据表时,需要用到ResultSet接口,它类似于一个数据表,通过该接口的实例可以获得检索结果集,以及对应数据表的接口信息。
复制代码
import java.sql.*;

public class SelectTable {
    
    public static void main(String[] args){
        try{
            //调用Class.forName()方法加载驱动程序
            Class.forName("com.mysql.jdbc.Driver");
            System.out.println("成功加载MySQL驱动!");
                
            String url="jdbc:mysql://localhost:3306/aniu";    //JDBC的URL    
            Connection conn;

            conn = DriverManager.getConnection(url,    "root","");
            Statement stmt = conn.createStatement(); //创建Statement对象
            System.out.println("成功连接到数据库!");

            String sql = "select * from stu";    //要执行的SQL
            ResultSet rs = stmt.executeQuery(sql);//创建数据对象
                System.out.println("编号"+"\t"+"姓名"+"\t"+"年龄");
                while (rs.next()){
                    System.out.print(rs.getInt(1) + "\t");
                    System.out.print(rs.getString(2) + "\t");
                    System.out.print(rs.getInt(3) + "\t");
                    System.out.println();
                }
                rs.close();
                stmt.close();
                conn.close();
            }catch(Exception e)
            {
                e.printStackTrace();
            }
    }
}
复制代码

3. 修改和删除数据库
复制代码
//修改删除数据
import java.sql.*;
public class UpdateDeleteDemo {
    public static void main(String[] args)throws Exception{
        try{
            //调用Class.forName()方法加载驱动程序
            Class.forName("com.mysql.jdbc.Driver");
            System.out.println("成功加载MySQL驱动!");
                
            String url="jdbc:mysql://localhost:3306/aniu";    //JDBC的URL    
            Connection conn;

            conn = DriverManager.getConnection(url,    "root","");
            Statement stmt = conn.createStatement(); //创建Statement对象
            System.out.println("成功连接到数据库!");

            //查询数据的代码
            String sql = "select * from stu";    //要执行的SQL
            ResultSet rs = stmt.executeQuery(sql);//创建数据对象
                System.out.println("编号"+"\t"+"姓名"+"\t"+"年龄");
                while (rs.next()){
                    System.out.print(rs.getInt(1) + "\t");
                    System.out.print(rs.getString(2) + "\t");
                    System.out.print(rs.getInt(3) + "\t");
                    System.out.println();
                }
                
            //修改数据的代码
            String sql2 = "update stu set name=? where number=?";
            PreparedStatement pst = conn.prepareStatement(sql2);
            pst.setString(1,"8888");
            pst.setInt(2,198);
            pst.executeUpdate();
                
            //删除数据的代码
            String sql3 = "delete from stu where number=?";
            pst = conn.prepareStatement(sql3);
            pst.setInt(1,701);
            pst.executeUpdate();
                
            ResultSet rs2 = stmt.executeQuery(sql);//创建数据对象
            System.out.println("编号"+"\t"+"姓名"+"\t"+"年龄");
            while (rs.next()){
                System.out.print(rs2.getInt(1) + "\t");
                System.out.print(rs2.getString(2) + "\t");
                System.out.print(rs2.getInt(3) + "\t");
                System.out.println();
            }
                
            rs.close();
            stmt.close();
            conn.close();
            }catch(Exception e)
            {
                e.printStackTrace();
            }
    }
}
复制代码

评论

此博客中的热门博文

javascript数组删除指定元素 remove index array specific

定义和用法 splice() 方法向/从数组中添加/删除项目,然后返回被删除的项目。 注释: 该方法会改变原始数组。 语法 arrayObject.splice(index,howmany,item1,.....,itemX) 参数 描述 index 必需。整数,规定添加/删除项目的位置,使用负数可从数组结尾处规定位置。 howmany 必需。要删除的项目数量。如果设置为 0,则不会删除项目。 item1, ..., itemX 可选。向数组添加的新项目。 返回值 类型 描述 Array 包含被删除项目的新数组,如果有的话。 说明 splice() 方法可删除从 index 处开始的零个或多个元素,并且用参数列表中声明的一个或多个值来替换那些被删除的元素。 如果从 arrayObject 中删除了元素,则返回的是含有被删除的元素的数组。

go url encoding

func  QueryUnescape func QueryUnescape (s string ) ( string , error ) QueryUnescape does the inverse transformation of QueryEscape, converting %AB into the byte 0xAB and '+' into ' ' (space). It returns an error if any % is not followed by two hexadecimal digits. func  QueryUnescape func QueryUnescape (s string ) ( string , error ) QueryUnescape does the inverse transformation of QueryEscape, converting %AB into the byte 0xAB and '+' into ' ' (space). It returns an error if any % is not followed by two hexadecimal digits.