预生命的登陆
package com.lianxi; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.Statement; import java.util.Scanner; public class TestSign { public static void main(String[] args) throws Exception { Class.forName("com.mysql.jdbc.Driver"); Connection conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/boooks","root","tiger"); Statement st=conn.createStatement(); Scanner s=new Scanner(System.in); System.out.println("用户名"); String name=s.nextLine(); System.out.println("密码"); String pwd=s.nextLine(); String sql="select * from tb_user where userName=? and password=?"; //创建ps PreparedStatement ps=conn.prepareStatement(sql); //赋值 ps.setString(1,name ); ps.setString(2, pwd); ResultSet rs=ps.executeQuery(); if(rs.next()){ System.out.println("登陆成功"); }else{ System.out.println("用户名或密码错误"); } rs.close(); st.close(); conn.close(); } }