1
2
3
4
5
6
7
try{
Connection
con
=
DriverManager.getConnection(url
,
username
,
password
)
}catch(SQLException
se){
System.out.println("数据库连接失败!")
se.printStackTrace()
}
希望可以帮助到你
package jdbcimport java.io.IOException
import java.io.InputStream
import java.sql.Connection
import java.sql.DriverManager
import java.sql.ResultSet
import java.sql.SQLException
import java.util.Properties
import java.sql.PreparedStatement
/**
* jdbc连接数据库, *** 作数据库的实体类
*
* @author yzm
*
*/
public class MySQLJDBCConnectionUtil {
private static String url// url路径
private static String driver// 驱动
private static String username// 用户名
private static String password// 密码
static {
try {
InputStream is = MySQLJDBCConnectionUtil.class.getClassLoader()
.getResourceAsStream("jdbc/jdbc.properties")
Properties prop = new Properties()
prop.load(is)
is.close()
url = prop.getProperty("url")
driver = prop.getProperty("driver")
username = prop.getProperty("username")
password = prop.getProperty("password")
Class.forName(driver)
} catch (IOException e) {
e.printStackTrace()
} catch (ClassNotFoundException e) {
e.printStackTrace()
}
}
/**
* 获得连接
*
* @return
* @throws SQLException
*/
public static Connection getConnection() throws SQLException {
return DriverManager.getConnection(url, username, password)
}
/**
* 关闭连接
*
* @param con
*/
public static void closeCon(Connection con) {
try {
if (con != null) {
con.close()
}
} catch (SQLException e) {
e.printStackTrace()
}
}
/**
* 关闭预编译
*
* @param stmt
*/
public static void closeStmt(PreparedStatement stmt) {
try {
if (stmt != null) {
stmt.close()
}
} catch (SQLException e) {
e.printStackTrace()
}
}
/**
* 关闭结果集
*
* @param rs
*/
public static void closeRs(ResultSet rs) {
try {
if (rs != null) {
rs.close()
}
} catch (SQLException e) {
e.printStackTrace()
}
}
/**
* 关闭没有结果集的连接
*
* @param con
* @param stmt
*/
public static void close(Connection con, PreparedStatement stmt) {
closeCon(con)
closeStmt(stmt)
}
/**
* 关闭有结果集的连接
*
* @param con
* @param stmt
* @param rs
*/
public static void close(Connection con, PreparedStatement stmt,
ResultSet rs) {
close(con, stmt)
closeRs(rs)
}
/**
* 测试主方法
*
* @param args
* @throws SQLException
*/
public static void main(String[] args) throws SQLException {
Connection c = getConnection()
System.out.println(c)
}
}
import java.sql.*public class getMetaData {
public static void main(String[] args) {
String driverClassName = "oracle.jdbc.OracleDriver"
String url = "jdbc:oracle:thin:@192.101.101.101:1521:orcl"
String password = "test"
String user= "test"
try {
Class.forName(driverClassName)
} catch (ClassNotFoundException ex) {
System.out.println("加载错误!")
}
Connection conn = null
try {
conn = DriverManager.getConnection(url, user, password)
System.out.println("连接成功")
String sql = "select id_ tid,name_ tname1 ,memo remark from tc"
PreparedStatement ps=conn.prepareStatement(sql,ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY)
ResultSet rs=ps.executeQuery()
ResultSetMetaData md = ps.getMetaData() //ps.getMetaData()//
for (int i = 1i <= md.getColumnCount()i++) {
System.out.println("name:"+md.getColumnName(i)+" label:"+md.getColumnLabel(i))
}
sql = "select max(id_) tid,name_ tname1 ,memo remark from tc group by name_,memo"
ps=conn.prepareStatement(sql,ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY)
rs=ps.executeQuery()
md = rs.getMetaData() //ps.getMetaData()//
for (int i = 1i <= md.getColumnCount()i++) {
System.out.println("name:"+md.getColumnName(i)+" label:"+md.getColumnLabel(i))
}
} catch (SQLException ex1) {
System.out.println(ex1)
System.out.println("失败")
}
}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)