YuFeng

JAVA JDBC 连接 MYSQL
工具包下载:jdbc驱动.zippackage cn.mySQL.test1; import java.sql....
扫描右侧二维码阅读全文
16
2018/09

JAVA JDBC 连接 MYSQL

工具包下载:
jdbc驱动.zip

package cn.mySQL.test1;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.PreparedStatement;

public class test1 {
    public static void main(String[] args) throws Exception {
        Class.forName("com.mysql.jdbc.Driver");   //激活驱动
        Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/mytest","root","密码");   //连接数据库获得连接
        String SQLString = "select * from user where uid = ?";   //SQL预编译语句
        PreparedStatement prepareStatement = connection.prepareStatement(SQLString);  //编译SQL语句
        prepareStatement.setInt(1, 1);   //设置预编译变量值
        ResultSet set = prepareStatement.executeQuery();   //获得结果
        while(set.next()) {
            System.out.print(set.getInt(1));
            System.out.println(set.getString(2));
        }
    }
}
Last modification:June 11th, 2019 at 12:28 pm
If you think my article is useful to you, please feel free to appreciate

Leave a Comment