想做一个j2se连接数据库的例子
但不知道怎么做
不知道有没有项目实例参考参考
谢谢
[b]问题补充:[/b]
有没有高人能给个这方面例子或参考文档呢
[b]问题补充:[/b]
谁能给个j2se连接数据库登陆之类的小例子呢
[b]问题补充:[/b]
就是swing这方面连接数据库之类的例子
[b]问题补充:[/b]
swing与数据库打交道的例子
[b]问题补充:[/b]
我的意思是说swing+数据库开发的实例
查看实例
http://www.builder.com.cn/2007/0823/467041.shtml
做个留言版联系联系吧 呵呵
我来给一个例子吧:
[code="java"]import java.sql.*; //引入相关的包
public class TestConnector {
/**
* @param args
*/
public static void main(String[] args) {
String url = "jdbc:mysql://127.0.0.1:3306/test"; //数据库的URL
Connection conn = null;
try {
Class.forName("com.mysql.jdbc.Driver"); //加载驱动程序
conn = DriverManager.getConnection(url, "root", ""); //取得连接
System.out.println("Connect the db successfully!");
} catch (ClassNotFoundException cnfe) {
cnfe.printStackTrace();
} catch (SQLException sqle) {
sqle.printStackTrace();
}
if (conn != null) {
try {
conn.close();
} catch (SQLException sqle) {
}
}
}
}
[/code]
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class Admin {
public static void main(
String args[]) {
}
public static Connection getConnetion(
String mode) {
String driver = "oracle.jdbc.OracleDriver";
String url = "jdbc:oracle:thin@localhost:1521:table";
String user = "user";
String password = "password";
// oracle
if (mode.equals("oracle")) {
driver = "oracle.jdbc.OracleDriver";
url = "jdbc:oracle:thin@localhost:1111:table";
}
// sql
else if (mode.equals("sql")) {
driver = "sun.jdbc.odbc.JdbcOdbcDriver";
url = "jdbc:odbc:table";
}
// MySql
else if (mode.equals("MySql")) {
driver = "com.mysql.jdbc.driver";
url = "jdbc:mysql://localhost:3306/table";
}
// access
else if (mode.equals("access")) {
driver = "sun.jdbc.odbc.JdbcOdbcDriver";
url = "jdbc:odbc:table";
}
// db2
else if (mode.equals("db2")) {
driver = "com.ibm.db2.jdbc.app.db2driver";
url = "jdbc:db2://localhost:5000/table";
}
Connection con = null;
try {
Class.forName(driver)
.getInterfaces();
con = DriverManager.getConnection(url, user, password);
con.setAutoCommit(false);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
return con;
}
}
参考文章