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
);
完毕!