急求Java小程序
请问谁有现成的JAVA 连接数据库的小程序,有的话能否给我一个,交个期末作业。感激不尽 --------------------编程问答-------------------- 网上下一个,很多的, --------------------编程问答-------------------- JAVA跑这个论坛找? --------------------编程问答-------------------- 跑这个论他照就错了! --------------------编程问答--------------------public class SelectData {--------------------编程问答-------------------- 就是一般的Java与数据库链接,与Applet关系不大 --------------------编程问答-------------------- 你可以在csdn资源里面找 --------------------编程问答--------------------
public static void main(String[] agrs) throws ClassNotFoundException,
FileNotFoundException {
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection conn = null;
//PreparedStatement pstmt = null;
PreparedStatement qpstmt = null;
int count = 0;
StringBuffer sb = new StringBuffer("");
try {
conn = DriverManager.getConnection(
"jdbc:oracle:.....", //
"username", //
"password" //
);
conn.setAutoCommit(false);// 手动提交
String qsql = "select count(*) as count_ from PUBLOGTMP.TMP_APPLY_POLICY_RELATION t where t.pas_apply_bar_code=?";
qpstmt = conn.prepareStatement(qsql);
ResultSet rs = qpstmt.executeQuery();
while(rs.next()) {
String count_ = rs.getString("count_");
}
conn.commit();
} catch (SQLException e) {
try {
if (conn != null)
conn.rollback();
} catch (SQLException ex) {
ex.printStackTrace();
}
e.printStackTrace();
} catch (BadCodeExecption e) {
try {
if (conn != null)
conn.rollback();
} catch (SQLException ex) {
ex.printStackTrace();
}
} finally {
if (qpstmt != null)
try {
qpstmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
if (conn != null)
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
--------------------编程问答-------------------- 这网上多的 把人能绊倒! --------------------编程问答--------------------
package sample;--------------------编程问答-------------------- package util;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class MysqlLianjie {
public static void main(String[] args) throws ClassNotFoundException {
String Driver = "com.mysql.jdbc.Driver";
String url="jdbc:mysql://localhost:3306/kjsb";
String username = "root";
String password = "long";
Connection con = null;
Statement s = null;
ResultSet rs = null;
try {
Class.forName(Driver);
con = DriverManager.getConnection(url, username, password);
System.out.println("数据库连接成功!");
s = con.createStatement();
// rs = s.executeQuery("select * from users");
String query = "create table consumer2(model INTEGER(4),speed INTEGER(4), ram INTEGER(4),hd INTEGER(4),price INTEGER(6))";
s.executeUpdate(query);
System.out.println("创建表成功!");
} catch (SQLException e) {
System.out.println("SQLException:" + e.getMessage());
}
}
}
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Properties;
import javax.sql.DataSource;
import org.apache.commons.dbcp.BasicDataSourceFactory;
public class DBUtil {
private static DataSource ds;
private static ThreadLocal<Connection> connLocal=new ThreadLocal<Connection>();
static{
Properties pro=new Properties();
try {
pro.load(DBUtil.class.getResource("util.properties").openStream());
ds=BasicDataSourceFactory.createDataSource(pro);
} catch (Exception e) {
e.printStackTrace();
}
}
public static Connection getConn() throws SQLException{
Connection conn=connLocal.get();
if(conn==null){
conn=ds.getConnection();
connLocal.set(conn);
}
return conn;
}
public static void closeConn() throws SQLException{
Connection conn=connLocal.get();
connLocal.set(null);
if(conn!=null){
conn.close();
}
}
public static void main(String[] args) throws SQLException {
System.out.println(getConn());
}
}
util.properties文件配置
driverClassName=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/库名?useUnicode=true&characterEncoding=utf8
username=用户名
password=密码
CookieTime=300
#初始连接池连接数量
initalSize=2
#最多存在的连接数
maxActive=15
#最大存在的空闲数量
maxIdle=2
#最少存在的空闲数量
minIdle=1
maxWait=30000 --------------------编程问答-------------------- 这种东西就不应该问 --------------------编程问答-------------------- 找本书照着写一下就会了
补充:Java , Web 开发