将文件
https://download.csdn.net/download/fighting_0808/10839026
如果是java文件,则新建一个文件把数据库连接包夹放在里面,然后点击这个链接文件右击build path---->Add to build path会产生一个导入包,添加成功。
DBMS.java
package test;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class DBMS {
static String url =null;
public static Connection connection() {
Connection con = null;
//驱动程序名
String driver = "com.mysql.jdbc.Driver";
//URL指向要访问的数据库名myfriend
String url = "jdbc:mysql://localhost:3306/myfriend";
//MySQL配置时的用户名
String user = "root";
//MySQL配置时的密码
String password = "123456";
try {
Class.forName(driver);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
try {
con = DriverManager.getConnection(url,user,password);
} catch (SQLException e) {
e.printStackTrace();
}
return con;
}
}
链接文件
数据库操作
查询
Connection con=DBMS.connection();
Statement stat1= con.createStatement();
String sql="select myfriend.userid,myfriend.name,myfriend.sex,myfriend.age,myfriend.qq,myfriend.telephone,myfriend.email,myfriend.address from user,myfriend where myfriend.userid=user.userid and user.name='"+name1+"'";
ResultSet rs= stat1.executeQuery(sql);
while(rs.next())
{
out.print("<tr>");
out.print("<td align=center>"+"<input type='radio' name='update'>"+"</td>");
out.print("<td align=center>"+"<input type='checkbox' name='delete'>"+"</td>");
out.print("<td align=center>"+rs.getInt("userid")+"</td>");
out.print("<td align=center>"+rs.getString("name")+"</td>");
out.print("<td align=center>"+rs.getString("sex")+"</td>");
out.print("<td align=center>"+rs.getInt("age")+"</td>");
out.print("<td align=center>"+rs.getString("qq")+"</td>");
out.print("<td align=center>"+rs.getString("telephone")+"</td>");
out.print("<td align=center>"+rs.getString("email")+"</td>");
out.print("<td align=center>"+rs.getString("address")+"</td>");
out.print("</tr align=center>");
}
rs.close();
con.close();
}
模糊查询
Statement stat2= con.createStatement();
String sql2="select myfriend.userid,myfriend.name,myfriend.sex,myfriend.age,myfriend.qq,myfriend.telephone,myfriend.email,myfriend.address from user,myfriend where myfriend.userid=user.userid and user.name='"+name1+"' and myfriend.name like '"+select+"%'";
ResultSet rs1= stat2.executeQuery(sql2);
while(rs1.next())
{
out.print("<tr>");
out.print("<td align=center>"+"<input type='radio' name='update'>"+"</td>");
out.print("<td align=center>"+"<input type='checkbox' name='delete'>"+"</td>");
out.print("<td align=center>"+rs1.getInt("userid")+"</td>");
out.print("<td align=center>"+rs1.getString("name")+"</td>");
out.print("<td align=center>"+rs1.getString("sex")+"</td>");
out.print("<td align=center>"+rs1.getInt("age")+"</td>");
out.print("<td align=center>"+rs1.getString("qq")+"</td>");
out.print("<td align=center>"+rs1.getString("telephone")+"</td>");
out.print("<td align=center>"+rs1.getString("email")+"</td>");
out.print("<td align=center>"+rs1.getString("address")+"</td>");
out.print("</tr align=center>");
}
rs1.close();
con.close();
}
web工程的导入位置