MySQL表格如何在netbeans的Jtable中输出
发布网友
发布时间:2023-04-11 05:15
我来回答
共1个回答
热心网友
时间:2023-09-20 16:23
连接数据库
public QQJDBCConnector() {
url="jdbc:mysql://localhost/rQQ?useUnicode=true&characterEncoding=utf-8";
user="root";
pws="104814";
conn=null;
}
public Connection getConnection() {
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
conn=DriverManager.getConnection(url,user,pws);
}
catch (Exception e) {
System.out.println("Error occurred obtaining connection");
System.out.println(e.getMessage());
}
return conn;
}
url="jdbc:mysql://localhost/rQQ?useUnicode=true&characterEncoding=utf-8";中rQQ表示你所要连接的数据库名
获取数据库内容:
public static String getUserName(String ID){
String name = "";
try{
conn = new QQJDBCConnector().getConnection();
stmt=conn.createStatement();
sql="select user.name from user where user.ID = '"+ID+"'";
rs=stmt.executeQuery(sql);
while(rs.next()) {
name=rs.getString("name");
}
stmt.close() ;
conn.close() ;
}
catch (Exception e){
System.out.println(e.getMessage());
}
return name;
}