1.连接
String userName="root"; //mysql用户名
String userPasswd="4622dqh"; //mysql密码
String dbName="test"; //数据库名
String tableName="user"; //表名
String url="jdbc:mysql://localhost/"+dbName+"?user="+userName+"&password="+userPasswd;
Class.forName("com.mysql.jdbc.Driver").newInstance();
Connection connection=DriverManager.getConnection(url);
Statement statement = connection.createStatement();
2.查询
String sql="SELECT * FROM "+tableName; //"sql查询语句"
ResultSet rs = statement.executeQuery(sql);
ResultSetMetaData rmeta = rs.getMetaData();
int numColumns=rmeta.getColumnCount();
out.print("id");
out.print("|");
out.print("num");
out.print("<br>");
while(rs.next()) {
out.print(rs.getString(1)+" ");
out.print("|");
out.print(rs.getString(2));
out.print("<br>");
}
out.print("<br>");
out.print("数据库操作成功,恭喜你");
关闭
rs.close();
statement.close();
connection.close();
网友评论