idea导入jar包
通过Project structure中的moudle添加
jdbc连接数据库
static final String JDBC_DRIVER = "com.mysql.jdbc.Driver"; static final String DB_URL = "jdbc:mysql://localhost:3306/daytest"; // 数据库的用户名与密码,需要根据自己的设置 static final String USER = "root"; static final String PASS = "123456"; public static void main(String[] args) { Connection conn = null; Statement stmt = null; PreparedStatement pstmt=null; try{ // 注册 JDBC 驱动 Class.forName(JDBC_DRIVER); // 打开链接 System.out.println("连接数据库..."); conn = DriverManager.getConnection(DB_URL,USER,PASS);
jdbc执行sql语句,用户输入参数执行
PreparedStatement pstmt=null;pstmt = conn.prepareStatement("SELECT * FROM user WHERE username=? AND password=?"); pstmt.setString(1,username); pstmt.setString(2,password); //System.out.println(sql); ResultSet rs = pstmt.executeQuery();