1. 求一個簡單又經典的java與資料庫例子,要有源代碼哦!
//下面的是連接mysql的例子
package com.song.struts.mySql;
import javax.swing.JComponent;
import java.sql.*;
import java.util.*;
// import com.borland.dx.sql.dataset.*;
public class mySqlDao extends JComponent {
private String UserName="root";
private String PWD="root";
private String url;
private Connection cn;
private Statement stmt;
private ResultSet rs = null;
public mySqlDao(){
try {
Class.forName("org.gjt.mm.mysql.Driver");
}
catch(java.lang.ClassNotFoundException e){
System.err.println("mydb() org.gjt.mm.mysql.Driver: " + e.getMessage());
}
catch(Exception e) {
e.printStackTrace();
}
}
//////////////////////////////
///返回mysql 連接,connection
/////////////////////////////
public Connection Connect(String dbname,String ip){
try{
String hostip=ip;
Properties myP = new Properties();
myP.setProperty("useUnicode","true");
myP.setProperty("characterEncoding","GB2312");
url="jdbc:mysql://"+hostip+":3306/"+dbname+"?user="+UserName+"&password="+PWD+"";
if(cn!=null){
cn.close();
}
cn=DriverManager.getConnection(url,myP);
stmt= cn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_UPDATABLE);
System.out.println("db connect success");
return cn;
}
catch(Exception e){
System.err.println("db connect err"+e.getMessage());
return null;
}
}
//////////////////////////////////
///關閉連接
/////////////////////////////////
public void close(){
try{
if(stmt!=null){
stmt.close();
}
if(cn!=null){
cn.close();
}
System.err.println("db colse success");
}
catch(Exception e){
System.err.println("db close err"+e.getMessage());
}
}
/////////////////////////////////////////////
// 用於進行記錄的查詢操�?,用於select 語句�?
//參數:sql語句�?
//返回:ResultSet對象
///////////////////////////////////////////
public ResultSet executeSelect(String sql) {
try {
stmt=cn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
rs = stmt.executeQuery(sql);
return rs;
}
catch(SQLException ex) {
System.err.println("db.executeQuery: " + ex.getMessage());
return null;
}
}
//////////////////////////////////////////////
//用於進行add或�?�update,insert,del等的記錄的操�?,
//入口參數:sql語句
//返回 :true,false
//////////////////////////////////////////////
public boolean executeUpdate(String sql) {
boolean bupdate=false;
try{
stmt=cn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
int rowCount = stmt.executeUpdate(sql);
if (rowCount!=0)
bupdate=true;
}
catch(SQLException ex) {
System.err.println("db.executeUpdate: " + ex.getMessage());
}
return bupdate;
}
//////////////////////////////////////////////
//用於進行表結構的操作,creat drop,modify等�??
//入口參數:sql語句
//返回 :true,false
//////////////////////////////////////////////
public boolean executeTable(String sql) {
boolean bupdate=false;
try {
stmt= cn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
System.out.print("對表的操作的sqlis :||"+sql+"||");
stmt.executeUpdate(sql);
bupdate=true;
}
catch(SQLException ex) {
System.err.println("db.executeTable: "+ex.getMessage());
}
return bupdate;
}
//////////////////////////
//返回資料庫的信息
//////////////////////////
public Statement getLWPAIDStatement(){
try{
return cn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
}
catch(java.sql.SQLException e){
System.err.println("getAISPStatement():"+e.getMessage());
return null;
}
}
public DatabaseMetaData getLWPAIDMetaData(){
try{
return cn.getMetaData();
}
catch(java.sql.SQLException e){
System.err.println("getAISPMetaData():"+e.getMessage());
return null;
}
}
public static void main(String args[]){
mySqlDao a=new mySqlDao();
a.Connect("mydb", "localhost");
int b=-100;
ResultSet rs=a.executeSelect("select max(bill_id) from t_bill limit 1");
try{
while(rs.next()){
System.out.println("is in");
b=rs.getInt(1);
}
}catch(Exception e){
e.printStackTrace();
}
System.out.println(b);
// java.util.Date date=new java.util.Date();
// System.out.println(date.toString());
// a.executeTable("insert into t_user values(100,'123','1345')");
// a.executeTable("update t_user set insert_date='"+date.toString()+"' where user_id=100");
a.close();
System.out.print(new pub().asc2unicode("�?!"));
}
}
2. 求:用Java連接資料庫和簡單的資料庫操作代碼
以上的代碼都不如哥的 且看哥是怎麼寫條理清晰的代碼的!!!
package dbconnection //java 中不存在沒有包的類(講解詳細因為項目需要)
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;//引入sql資料庫包
public class DBConnection{
private Connection conn=null;
private Statement stmt=null;
private Result rs=null;
private String jdbc="com.microsoft.sqlserver.jdbc.SQLServerDriver";
private String driverManager="jdbc:sqlserver://localhost:1433;databasename=HcitPos";
private String user="admin";
private int password="admin";
public DBConnection{
try{
Class.forName("jdbc");
conn.getConnection("driverManager");
}
catch(Exception e){}
}
public selectMethod(String sql){
stmt=conn.createStatement();
rs=stmt.extcuteQuery("sql");
while(rs.next()){
String title=rs.getString("title");//利用javaBean獲得資料庫中的屬性
String name=rs.getString("name");
.......
System.out.println("title");
System.out.println("name");
......
//當然資料庫的操作有很多 這里簡單介紹下功能的實現
}
}
public void closeDB(){
if(rs != null) rs.close();
if(stmt != null) stmt.close();
if(conn != null) conn.close();
}
}
3. SQL資料庫用代碼創建 怎麼創建
createdatabase資料庫名稱
on
(
name='aaa',
filename='aaa.dbf',
size=初始大小,
maxsize=存儲上限,
filegrowth=增長增量
)
logon
(
name='b',
filename='b.ldf',
size=初始大小,
maxsize=存儲上限,
filegrowth=增長增量
)
SQL語句創建基本資料庫
SQL語句代碼:CREATEDATABASE語句。
CREATEDATABASEEpiphany
ON
(
NAME=Epiphany,
FILENAME='E:SQLSERVER2008Epiphany_data.mdf',
SIZE=5MB,
MAXSIZE=20,
FILEGROWTH=20
)
LOGON
(
NAME=Epiphany,
FILENAME='E:SQLSERVER2008Epiphany_log.ldf',
SIZE=2MB,
MAXSIZE=10MB,
FILEGROWTH=1MB
);
可以用這個模板,例子:
CREATEDATABASEEpiphany
ON
(
NAME=Epiphany,
FILENAME='E:SQLSERVER2008Epiphany_data.mdf',
SIZE=5MB,
MAXSIZE=20,
FILEGROWTH=20
)
LOGON
(
NAME=Epiphany,
FILENAME='E:SQLSERVER2008Epiphany_log.ldf',
SIZE=2MB,
MAXSIZE=10MB,
FILEGROWTH=1MB
);
完畢!