java连接数据库的例子
- 提问者网友:听门外雪花风
- 2021-04-11 09:55
- 五星知识达人网友:西岸风
- 2021-04-11 11:30
ConnectionManager.java
import java.sql.*;
public class ConnectionManager {
private static final String DRIVER_CLASS="com.microsoft.sqlserver.jdbc.SQLServerDriver";
private static final String DATABASE_URL="jdbc:sqlserver://localhost:1433;DatabaseName=student";
private static final String DATABASE_USER="xz";
private static final String DATABASE_PASSWORD="test";
private ConnectionManager()
{}
public static Connection getConnection()
{
Connection dbConnection=null;
try {
Class.forName(DRIVER_CLASS);
dbConnection=DriverManager.getConnection(DATABASE_URL,DATABASE_USER,DATABASE_PASSWORD);
} catch (ClassNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return dbConnection;
}
public static void closeConnection(Connection conn)
{
try{
if(conn!=null && !conn.isClosed())
{
conn.close();
conn=null;
}
}catch(Exception ex)
{
System.out.println(ex.getMessage());
}
}
public static void closeResultSet(ResultSet rs)
{
try{
if(rs!=null)
{
rs.close();
rs=null;
}
}catch(Exception ex)
{
System.out.println(ex.getMessage());
}
}
public static void closePreparedStatement(PreparedStatement pstmt)
{
try{
if(pstmt!=null)
{
pstmt.close();
pstmt=null;
}
}catch(Exception ex)
{
System.out.println(ex.getMessage());
}
}
public static void closeStatement(Statement stmt)
{
try{
if(stmt!=null)
{
stmt.close();
stmt=null;
}
}catch(Exception ex)
{
System.out.println(ex.getMessage());
}
}
public static void main(String[]args)
{
try {
Connection con=ConnectionManager.getConnection();
if(!con.isClosed())
System.out.println("数据库连接成功。。。。。");
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
User.java
public class User {
private String username;
private String userpass;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getUserpass() {
return userpass;
}
public void setUserpass(String userpass) {
this.userpass = userpass;
}
}
UserDAO.java
import java.util.*;
import java.sql.*;
public class UserDAO {
public List<User> getAllUser()
{
Connection conn=null;
PreparedStatement pstmt=null;
ResultSet rs=null;
List<User> userList=new ArrayList<User>();
String sql="select UserName,UserPas from Users";
try {
conn=ConnectionManager.getConnection();
pstmt=conn.prepareStatement(sql);
rs=pstmt.executeQuery();
while(rs.next())
{
User u=new User();
u.setUsername(rs.getString("UserName"));
u.setUserpass(rs.getString("UserPas"));
userList.add(u);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally
{
ConnectionManager.closeConnection(conn);
ConnectionManager.closePreparedStatement(pstmt);
ConnectionManager.closeResultSet(rs);
}
return userList;
}
}
- 1楼网友:深街酒徒
- 2021-04-11 14:52
Java连接数据库实例
此文中的代码主要列出连接数据库的关键代码,其他访问数据库代码省略 1、Oracle8/8i/9i数据库(thin模式) Class.forName("oracle.jdbc.driver.OracleDriver").newInstance(); String url="jdbc:oracle:thin:@localhost:1521:orcl"; //orcl为数据库的SID String user="test"; String passWord="test"; Connection conn= DriverManager.getConnection(url,user,password); 2、DB2数据库 Class.forName("com.ibm.db2.jdbc.app.DB2Driver ").newInstance(); String url="jdbc:db2://localhost:5000/sample"; //sample为你的数据库名 String user="admin"; String password=""; Connection conn= DriverManager.getConnection(url,user,password); 3、Sql Server7.0/2000数据库 Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver").newInstance(); String url="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=mydb"; //mydb为数据库 String user="sa"; String password=""; Connection conn= DriverManager.getConnection(url,user,password); 4、Sybase数据库 Class.forName("com.sybase.jdbc.SybDriver").newInstance(); String url =" jdbc:sybase:Tds:localhost:5007/myDB"; //myDB为你的数据库名 Properties sysProps = System.getProperties(); SysProps.put("user","userid"); SysProps.put("password","user_password"); Connection conn= DriverManager.getConnection(url, SysProps); 5、Informix数据库 Class.forName("com.informix.jdbc.IfxDriver").newInstance(); String url = "jdbc:informix-sqli://123.45.67.89:1533/myDB:INFORMIXSERVER=myserver; user=testuser;password=testpassword"; //myDB为数据库名 Connection conn= DriverManager.getConnection(url); 6、MySQL数据库 Class.forName("org.gjt.mm.mysql.Driver").newInstance(); String url ="jdbc:mysql://localhost/myDB?user=soft&password=soft1234&useUnicode=true&characterEncoding=8859_1" //myDB为数据库名 Connection conn= DriverManager.getConnection(url); 7、PostgreSQL数据库 Class.forName("org.postgresql.Driver").newInstance(); String url ="jdbc:postgresql://localhost/myDB" //myDB为数据库名 String user="myuser"; String password="mypassword"; Connection conn= DriverManager.getConnection(url,user,password);
- 2楼网友:山河有幸埋战骨
- 2021-04-11 13:19
public 返回对象 方法名()
{
//创建数据库连接
Connection con = JDBC.getConnection();
string sql="select * from 表名";
//创建预处理器 PreparedStatement pre = con.prepareStatement(sql); //执行sql语句 ResultSet rs = pre.executeQuery();
if(rs.read())
{
对象 u= new 对象();
//设置值
u.属性=rs.get类型("字段名");
}
//关闭结果集
rs.close();
//关闭连接
conn.close();
}
- 3楼网友:琴狂剑也妄
- 2021-04-11 12:34
public List getObject(String sql, Object[] object) { //sql执行语句,object是你sql语句里面的参数 List list = new ArrayList(); Connection con = null; PreparedStatement pre = null; ResultSet rs = null; try{ con = C3P0Util.getInstance().getConnection(); //这是你获得数据库连接,你把这里改成调用你自己写的jdbc方法 pre = con.prepareStatement(sql); //执行sql语句 if(object!=null){ for(int i=0;i<object.length;i++){ pre.setObject(i+1, object[i]); //给sql里面的参数进行赋值 } } rs = pre.executeQuery(); while(rs.next()){ Users u = new User(); u.setUserName(rs.getString("UserName")); u.setUserPas(rs.getString("UserPas"));
list.add(u); } }catch(Exception e){ e.printStackTrace(); return null; }finally{ C3P0Util.close(con, pre, rs); //关闭数据库资源 } return list; //返回list集合 }
注意:list里面保存的是User对象的信息 你要获得User对象的信息,那么就要遍历list for(int i=0;i<list.size;i++){ User u = (User)list.get(i);
System.out.println("UserName:"+u.getUserName()); System.out.println("UserPas:"+u.getUserPas()); }
上面是针对list里面有很多个User对象,当然list里面只有一个User对象,也是可以的。 如果你的list里面只有一个User,那么可以直接:User u = (User)list.get(0); System.out.println("UserName:"+u.getUserName()); System.out.println("UserPas:"+u.getUserPas()); 希望对你有帮助!