java中定义一个类,主要从数据库中获取字段怎么编写

java中定义一个类,主要从数据库中获取字段怎么编写,第1张

首先连接数据库,你也没说什么数据库,给你三种连接,

package com.cai.jdbc

import java.sql.Connection

import java.sql.DriverManager

import java.util.Properties

public class ConnectionUtil {

/**

* 1

*在方法中固化连接参数

* @return 数据库连接

*/

public Connection getConnection(){

Connection conn = null

try{

Class.forName("com.mysql.jdbc.Driver")

conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/stud", "root", "root")

return conn

}catch(Exception e){

e.printStackTrace()

}

return null

}

/**

* 2

*通过方法参数方式传递连接参数

* @return 数据库连接

*/

public Connection getConnection(String driver, String url, String user, String password){

Connection conn = null

try{

Class.forName(driver)

conn = DriverManager.getConnection(url, user, password)

return conn

}catch(Exception e){

e.printStackTrace()

}

return null

}

/**

* 3

*通过properties配置文件的方式灵活配置连接参数,properties中的属性名固化

* @return 数据库连接

*/

public Connection openConnection(){

Connection conn = null

String driver = ""

String url = ""

String user = ""

String password = ""

Properties props = new Properties()

try{

props.load(this.getClass().getClassLoader().getResourceAsStream("DBConfig.properties"))

driver = props.getProperty("driver")

url = props.getProperty("url")

user = props.getProperty("user")

password = props.getProperty("password")

Class.forName(driver)

conn = DriverManager.getConnection(url, user, password)

return conn

}catch(Exception e){

e.printStackTrace()

}

return null

}

public static void main(String []args){

ConnectionUtil cu = new ConnectionUtil()

System.out.println("1、---->" + cu.getConnection())

System.out.println("2、---->" + cu.getConnection("com.mysql.jdbc.Driver",

"jdbc:mysql://localhost:3306/stud", "root", "root"))

System.out.println("3、---->" + cu.openConnection())

}

}

连接上数据库就可以 *** 作数据了,在写sql语句 根据sql执行就有返回相应字段值了

Java获取数据库的表中各字段的字段名,代码如下:

import java.sql.Connectionimport java.sql.DriverManagerimport java.sql.ResultSetimport java.sql.PreparedStatementimport java.sql.ResultSetMetaDataimport java.sql.SQLExceptionpublic class TestDemo {public static Connection getConnection() {Connection conn = nulltry {Class.forName("com.mysql.jdbc.Driver")String url = "jdbc:mysql://数据库IP地址:3306/数据库名称"String user = "数据库用户名"String pass = "数据库用户密码"conn = DriverManager.getConnection(url, user, pass)} catch (ClassNotFoundException e) {e.printStackTrace()} catch (SQLException e) {e.printStackTrace()}return conn}public static void main(String[] args) {Connection conn = getConnection()String sql = "select * from AccessType"PreparedStatement stmttry {stmt = conn.prepareStatement(sql)ResultSet rs = stmt.executeQuery(sql)ResultSetMetaData data = rs.getMetaData()for (int i = 1i <= data.getColumnCount()i++) {// 获得所有列的数目及实际列数int columnCount = data.getColumnCount()// 获得指定列的列名String columnName = data.getColumnName(i)// 获得指定列的列值int columnType = data.getColumnType(i)// 获得指定列的数据类型名String columnTypeName = data.getColumnTypeName(i)// 所在的Catalog名字String catalogName = data.getCatalogName(i)// 对应数据类型的类String columnClassName = data.getColumnClassName(i)// 在数据库中类型的最大字符个数int columnDisplaySize = data.getColumnDisplaySize(i)// 默认的列的标题String columnLabel = data.getColumnLabel(i)// 获得列的模式String schemaName = data.getSchemaName(i)// 某列类型的精确度(类型的长度)int precision = data.getPrecision(i)// 小数点后的位数int scale = data.getScale(i)// 获取某列对应的表名String tableName = data.getTableName(i)// 是否自动递增boolean isAutoInctement = data.isAutoIncrement(i)// 在数据库中是否为货币型boolean isCurrency = data.isCurrency(i)// 是否为空int isNullable = data.isNullable(i)// 是否为只读boolean isReadOnly = data.isReadOnly(i)// 能否出现在where中boolean isSearchable = data.isSearchable(i)System.out.println(columnCount)System.out.println("获得列" + i + "的字段名称:" + columnName)System.out.println("获得列" + i + "的类型,返回SqlType中的编号:"+ columnType)System.out.println("获得列" + i + "的数据类型名:" + columnTypeName)System.out.println("获得列" + i + "所在的Catalog名字:"+ catalogName)System.out.println("获得列" + i + "对应数据类型的类:"+ columnClassName)System.out.println("获得列" + i + "在数据库中类型的最大字符个数:"+ columnDisplaySize)System.out.println("获得列" + i + "的默认的列的标题:" + columnLabel)System.out.println("获得列" + i + "的模式:" + schemaName)System.out.println("获得列" + i + "类型的精确度(类型的长度):" + precision)System.out.println("获得列" + i + "小数点后的位数:" + scale)System.out.println("获得列" + i + "对应的表名:" + tableName)System.out.println("获得列" + i + "是否自动递增:" + isAutoInctement)System.out.println("获得列" + i + "在数据库中是否为货币型:" + isCurrency)System.out.println("获得列" + i + "是否为空:" + isNullable)System.out.println("获得列" + i + "是否为只读:" + isReadOnly)System.out.println("获得列" + i + "能否出现在where中:"+ isSearchable)}} catch (SQLException e) {e.printStackTrace()}}}


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/sjk/9601486.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-30
下一篇 2023-04-30

发表评论

登录后才能评论

评论列表(0条)

保存