struts怎样连接mysql
展开全部
底层还是利用jdbc连接。
下载: mysql-connector-java-5.1.6-bin.jar
连接代码
boolean flag=false;
Connection conn = null;
ResultSet rs = null;
Statement stmt = null;
String DBDriver = "com.mysql.jdbc.Driver";
try{
Class.forName(DBDriver);
conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/login", "root", "root");
stmt=conn.createStatement();
System.out.println("测试连接!");
System.out.println(username);
rs = stmt.executeQuery("select * from user where username ='" + username +"' and password = '"+ password+"'");
if(rs.next()){
System.out.println("登录成功!");
flag=true;
}
}catch(Exception e){
System.out.print("连接错误!");
e.printStackTrace();
}
return flag;
}
下载: mysql-connector-java-5.1.6-bin.jar
连接代码
boolean flag=false;
Connection conn = null;
ResultSet rs = null;
Statement stmt = null;
String DBDriver = "com.mysql.jdbc.Driver";
try{
Class.forName(DBDriver);
conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/login", "root", "root");
stmt=conn.createStatement();
System.out.println("测试连接!");
System.out.println(username);
rs = stmt.executeQuery("select * from user where username ='" + username +"' and password = '"+ password+"'");
if(rs.next()){
System.out.println("登录成功!");
flag=true;
}
}catch(Exception e){
System.out.print("连接错误!");
e.printStackTrace();
}
return flag;
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询