跳至主要内容

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();
            }
    }
}
复制代码

评论

此博客中的热门博文

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.

go golang get disk usage free space remain info

package main import ( "fmt" "syscall" ) func main() { fmt.Println(DiskUsage("./")) } func DiskUsage(path string) uint64 { fs := syscall.Statfs_t{} err := syscall.Statfs(path, &fs) if err != nil { return 0 } return fs.Bfree * uint64(fs.Bsize) } //All space   = fs.Blocks * uint64(fs.Bsize) //Free space = fs.Bfree * uint64(fs.Bsize) //Used space= fs.All - disk.Free

go mobile setup

Preparing: 1.Install NDK 2.Install Android-Studio  &&  SDK (with env ANDROID_HOME) 3.Install Go / git Step 1: $ go get golang.org/x/mobile/cmd/gomobile $gomobile init -ndk ~/Android/Ndk Native App: 1.Android basic/main.go package main import ( "log" "golang.org/x/mobile/app" "golang.org/x/mobile/event/lifecycle" "golang.org/x/mobile/event/paint" ) func main() { app.Main(func(a app.App) { for e := range a.Events() { switch e := a.Filter(e).(type) { case lifecycle.Event: // ... case paint.Event: log.Print("Call OpenGL here.") a.Publish() } } }) } $gomobile build -target=android  basic/ $adb install basic.apk 2.iOS: $gomobile build -target=android  basic/ SDK App: hello/hello.go package hello import "fmt" func Greetings(name string) string { return fmt.Sprintf("Hello, %s!", name) } 1.Android: $ gomobile   bind   -target=android    hello/ ...