怎么用java查数据库中表的列名?
把tableName换成你的表名,有些数据库也可以直接使用Sql查询表列名 ResultSet resultSet = statement.executeQuery("select * from tableName"); ResultSetMetaData resultSetMetaData = resultSet.getMetaData(); for(int i=0; i<resultSetMetaData.getColumnCount(); i++) { System.out.print( resultSetMetaData.getColumnLabel(i) + "\t" ); System.out.print( resultSetMetaData.getColumnName(i) + "\t" ); System.out.println( resultSetMetaData.getColumnTypeName(i) ); }问友好好学java呀
怎么用java查数据库中表的列名
可以用下面的方式获取,把tableName换成你的表名,有些数据库也可以直接使用Sql查询表列名
ResultSet resultSet = statement.executeQuery("select * from tableName");
ResultSetMetaData resultSetMetaData = resultSet.getMetaData();
for(int i=0; i<resultSetMetaData.getColumnCount(); i++) {
System.out.print( resultSetMetaData.getColumnLabel(i) + "\t" );
System.out.print( resultSetMetaData.getColumnName(i) + "\t" );
System.out.println( resultSetMetaData.getColumnTypeName(i) );
}
java 如何连接数据库,如何完成数据库记录添加,删除,修改以及查询。
其实,也就那几步而已:别忘了,加载jar文件加载驱动获得Connection获得PreparedStatement /PreparedStatement执行sql语句处理结果集ResultSet关闭连接mport java.sql.Connection;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.util.ArrayList;import java.util.List;import Entity.JIAJU;public class JiaJu { public JIAJU selectExe(int shouhinId) { JIAJU jia = new JIAJU(); try { Connection con = ConnectionManager.getConnection(); String sql = "select * from jiaju where shouhinId=?"; PreparedStatement ps = con.prepareStatement(sql); ps.setInt(1, shouhinId); ResultSet rs = ps.executeQuery(); if (rs != null) { while (rs.next()) { jia.setShouhinId(rs.getInt("shouhinId")); jia.setShouhinName(rs.getString("shouhinName")); jia.setShouhinColor(rs.getString("shouhinColor")); jia.setShouhinPrice(rs.getInt("shouhinPrice")); jia.setShouhinPai(rs.getString("shouhinPai")); jia.setShouhinShi(rs.getString("shouhinShi")); // list.add(jia); } } } catch (Exception e) { e.printStackTrace(); } return jia; } public void insertJia(JIAJU jia) { try { Connection con = ConnectionManager.getConnection(); String sql = "insert into jiaju values(?,?,?,?,?)"; PreparedStatement ps = con.prepareStatement(sql); ps.setString(1, jia.getShouhinName()); ps.setString(2, jia.getShouhinColor()); ps.setInt(3, jia.getShouhinPrice()); ps.setString(4, jia.getShouhinPai()); ps.setString(5, jia.getShouhinShi()); ps.executeUpdate(); } catch (Exception e) { e.printStackTrace(); } } public List selectJia() { List list = new ArrayList(); try { Connection con = ConnectionManager.getConnection(); String sql = "select * from jiaju "; PreparedStatement ps = con.prepareStatement(sql); ResultSet rs = ps.executeQuery(); if (rs != null) { while (rs.next()) { JIAJU jia = new JIAJU(); jia.setShouhinId(rs.getInt("shouhinId")); jia.setShouhinName(rs.getString("shouhinName")); jia.setShouhinColor(rs.getString("shouhinColor")); jia.setShouhinPrice(rs.getInt("shouhinPrice")); jia.setShouhinPai(rs.getString("shouhinPai")); jia.setShouhinShi(rs.getString("shouhinShi")); list.add(jia); } } } catch (Exception e) { e.printStackTrace(); } return list; } public JIAJU selectbuy(int shouhinId) { JIAJU jia = new JIAJU(); try { Connection con = ConnectionManager.getConnection(); String sql = "select * from jiaju where shouhinId=?"; PreparedStatement ps = con.prepareStatement(sql); ps.setInt(1, shouhinId); ResultSet rs = ps.executeQuery(); if (rs != null) { while (rs.next()) { jia.setShouhinId(rs.getInt("shouhinId")); jia.setShouhinName(rs.getString("shouhinName")); jia.setShouhinColor(rs.getString("shouhinColor")); jia.setShouhinPrice(rs.getInt("shouhinPrice")); jia.setShouhinPai(rs.getString("shouhinPai")); jia.setShouhinShi(rs.getString("shouhinShi")); } } } catch (Exception e) { e.printStackTrace(); } return jia; } public void updateLou(JIAJU jia){ try{ Connection con = ConnectionManager.getConnection(); String sql = "update jiaju set shouhinPrice=? where shouhinId=?"; PreparedStatement ps = con.prepareStatement(sql); ps.setInt(1,jia.getShouhinPrice()); ps.setInt(2, jia.getShouhinId()); ps.executeUpdate(); }catch(Exception e){ e.printStackTrace(); } } public void deleteLou(JIAJU jia){ try{ Connection con = ConnectionManager.getConnection(); String sql = "delete from jiaju where shouhinId=?"; PreparedStatement ps = con.prepareStatement(sql); ps.setInt(1, jia.getShouhinId()); ps.executeUpdate(); }catch(Exception e){ e.printStackTrace(); } }}
如何用java查询出MySQL中的所有表名
第一步:添加JDBC驱动程序包(mysql-connector-java-5.0.4-bin.jar 这个去网上找!)
第二步:import java.sql.* ;
第三步:加载并注册驱动程序!
Class.forName("com.mysql.jdbc.Driver");
第四步:创建一个Connection对象!
DriverManager.getConnection("jdbc:mysql://localhost/test?user=root&password=root");
第五步:创建一个 Statement 对象或preparedStatement对象
Statement stmt = conn.createStatement();
第六步:执行语句!
ResultSet rs = stmt.executeQuery("show tables; ");
第七步:使用ResultSet对象!
while (rs.next()) {
System.out.println(rs.getString(“ename"));
}
关闭ResultSet 对象
关闭Statement对象或
preparedStatement对象
关闭连接
java 获取mysql 某个数据库中所有表及表的列的信息
mysql里面提供了很多方法来获取表结构和表列:如下方法
获得某表所有列的信息:
String sql = select * from tname;//tname为某一表名
Connection conn = ....;
Statement st = conn.createStatement();
ResultSet rs = st.rs = st.executeQuery(sql);
ResultSetMetaData rsmd = rs.getMetaData();
int colcount = rsmd.getColumnCount();//取得全部列数
for(int i=0;i<colcount;i++){
String colname = rsmd.getColumnName(i);//取得全部列名
}
以上为某表字段具体查询,如果是查询表的信息,如在mysql服务器上那样的查询结果的话,可以用一下代码:
ResultSet.executeQuery("show tables")可以的到所有的表信息。
ResultSet.executeQuery("describe tname")可以得到表的字段信息。//tname为表名