利用反射机制查看包是否被导入,idea可以用maven导入依赖,
Class.forName(“com.mysql.cj.jdbc.Driver”);
maven代码如下:
mysql mysql-connector-java8.0.25 runtime
粘贴到pom.xml里面然后导入一下。
这里设置链接地址 jdbc:mysql://127.0.0.1代表链接本机,3306是端口号,supermaket 数据库名 ,characterEncoding = UTF-8编码格式
String url = “jdbc:mysql://127.0.0.1:3306/supermaket?characterEncoding = UTF-8”;
建立链接
Connection conn = DriverManager.getConnection(url, “root”, “11”);
public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.println("请输入用户名"); String uName = scanner.next(); System.out.println("请输入密码"); String uTel = scanner.next(); try{ Class.forName("com.mysql.cj.jdbc.Driver"); String url = "jdbc:mysql://127.0.0.1:3306/supermaket?characterEncoding = UTF-8"; Connection conn = DriverManager.getConnection(url, "root", "11"); // String sql = "insert into user values(null,'蒲林之',0,'123')"; String sql1 = "select * from user where uName ='"+uName+"'and tel='"+uTel+"'"; Statement statement = conn.createStatement(); // int i = statement.executeUpdate(sql); ResultSet resultSet = statement.executeQuery(sql1); resultSet.next(); int anInt = resultSet.getInt(1); if (anInt>0){ String name = resultSet.getString(2); System.out.println("欢迎您"+name); }else { System.out.println("登录失败"); } // System.out.println(i); statement.close(); conn.close(); }catch (Exception e){ System.out.println("程序错误"); } }
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)