java中怎样对mysql数据库查询
展开全部
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.Statement;
import java.util.Hashtable;
public class GetDBFiled {
public static void conn() {
String driver = "com.mysql.jdbc.Driver";
String url = "jdbc:mysql://localhost:3306/databaseName";
String user = "root";
String password = "密码";
try {
Class.forName(driver);
Connection conn = DriverManager.getConnection(url, user, password);
if (!conn.isClosed()) {
String sql = "select * from pic";
System.out.println("Succeeded connecting to the Database!");
Statement statement =conn.createStatement();
ResultSet rs = statement.executeQuery(sql);
if (rs.next()) {
// 得到ResultSetMetaData
ResultSetMetaData rsmd = rs.getMetaData();
System.out.println(rsmd.getColumnCount());
for (int i = 1; i <= rsmd.getColumnCount(); i++) {
Hashtable hst = new Hashtable();
// 把字段名放入Name
String name = String.valueOf(rsmd.getColumnLabel(i));
hst.put("Name", name);
// 把字段类型放入Type
String type = String.valueOf(rsmd.getColumnType(i));
hst.put("Type", type);
System.out.println(hst.get("Name")+" "+hst.get("Type"));
}
}
}
} catch (Exception e) {
System.out.println("出现异常");
}
}
}
展开全部
import java.sql.*;
public class MySqlJDBC {
public static void main(String args[]) {
try {
Class.forName("com.mysql.jdbc.Driver"); // 加载对应 JDBC驱动程序
System.out.println("Success loading Mysql Driver!");
} catch (Exception e) {
System.out.print("Error loading Mysql Driver!");
e.printStackTrace();
}
try {
Connection connect = DriverManager.getConnection(
"jdbc:mysql://localhost:4706/sdkcc", "root", "123456");
// 连接URL为 jdbc:mysql//服务器地址/数据库名
// 后面的2个参数分别是登陆用户名和密码
System.out.println("Success connect Mysql server!");
Statement stmt = connect.createStatement();
ResultSet rs = stmt.executeQuery("select * from actor");
while (rs.next()) {
System.out.println(rs.getString("actor_id") + "\t"
+ rs.getString("first_name") + "\t" + "\t"
+ rs.getString("last_name") + "\t"
+ rs.getString("last_update"));
}
} catch (Exception e) {
System.out.print("get data error!");
e.printStackTrace();
}
}
}
也可以自己上网查下资料,这很容易查到!
public class MySqlJDBC {
public static void main(String args[]) {
try {
Class.forName("com.mysql.jdbc.Driver"); // 加载对应 JDBC驱动程序
System.out.println("Success loading Mysql Driver!");
} catch (Exception e) {
System.out.print("Error loading Mysql Driver!");
e.printStackTrace();
}
try {
Connection connect = DriverManager.getConnection(
"jdbc:mysql://localhost:4706/sdkcc", "root", "123456");
// 连接URL为 jdbc:mysql//服务器地址/数据库名
// 后面的2个参数分别是登陆用户名和密码
System.out.println("Success connect Mysql server!");
Statement stmt = connect.createStatement();
ResultSet rs = stmt.executeQuery("select * from actor");
while (rs.next()) {
System.out.println(rs.getString("actor_id") + "\t"
+ rs.getString("first_name") + "\t" + "\t"
+ rs.getString("last_name") + "\t"
+ rs.getString("last_update"));
}
} catch (Exception e) {
System.out.print("get data error!");
e.printStackTrace();
}
}
}
也可以自己上网查下资料,这很容易查到!
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询