java对数据库操作代码,使用java实现数据库编程第一章上机
如何通过Java代码操作数据库
//此类为连接数据库并进行数据库的操作
10多年的大石桥网站建设经验,针对设计、前端、开发、售后、文案、推广等六对一服务,响应快,48小时及时工作处理。全网整合营销推广的优势是能够根据用户设备显示端的尺寸不同,自动调整大石桥建站的显示方式,使网站能够适用不同显示终端,在浏览器中调整网站的宽度,无论在任何一种浏览器上浏览网站,都能展现优雅布局与设计,从而大程度地提升浏览体验。创新互联从事“大石桥网站设计”,“大石桥网站推广”以来,每个客户项目都认真落实执行。
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class Conn {
private static Connection conn = null;
private static Statement st = null;
private static ResultSet rs = null;
//建立数据库的连接
java连接数据库具体操作以及代码!最好有个例子 越详细越好?
你要连接的是什么数据库
连接什么数据库,你就要去下载相应的驱动
package com.dao.conn;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import com.entity.PubTermBean;
/**
* ms sql2005连接
* @author Administrator
*
*/
public class ConnByMsSql2005 {
public static Connection getConn(){
Connection conn=null;
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver").newInstance(); //加载驱动类
conn=DriverManager.getConnection("jdbc:sqlserver:127.0.0.1:1433;databasename=数据库名","登录数据库名","密码");
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
return conn;
}
}
//查询数据库表的方法
public ListUs select() {
Connection con = null;
PreparedStatement pst = null;
ResultSet rt = null;
con = ConnByMsSql2005 .getConn();//获得上面的连接数据库的对象
ListUs list = new ArrayListUs();
try {
String sql="select * from users";//查询语句
pst = con.prepareStatement();
rt = pst.executeQuery();//执行查询
while (rt.next()) { //循环查询的结果集
//将数据封装到javabean里
Us u = new Us();
u.setId(rt.getInt(1));
u.setName(rt.getString(2));
u.setPwd(rt.getString(3));
//将每一个javabean对象放到list集合
list.add(u);
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
//关闭数据库操作对象
try {
if (rt != null) {
rt.close();
}
if (pst != null) {
pst.close();
}
if (con != null) {
con.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
return list; //返回得到的数据库数据
}
通过java代码如何实现对mysql数据库进行创建新的数据库的操作
1 import java.sql.Connection;
2 import java.sql.DriverManager;
3 import java.sql.ResultSet;
4 import java.sql.SQLException;
5 import java.sql.Statement;
6
7 public class CreateDataSource {
8
9 /**
10 * @param args
11 */
12 public static void main(String[] args) {
13 // TODO Auto-generated method stub
14 String database = "test2";
15 new CreateDataSource().getConn(database);
16 }
17
18 String mysqlDriver = "com.mysql.jdbc.Driver";
19 String url = "jdbc:mysql://localhost:3306/test1";
20 String newUrl = "jdbc:mysql://localhost:3306/";
21 String username = "root";
22 String password = "root";
23 Connection conn = null;
24 Connection newConn = null;
25
26 public Connection getConn(String database) {
27
28 try {
29 Class.forName(mysqlDriver);
30 } catch (ClassNotFoundException e) {
31 // TODO Auto-generated catch block
32 e.printStackTrace();
33 }
34 try {
35 String tableSql = "create table t_user (username varchar(50) not null primary key,"
36 + "password varchar(20) not null ); ";
37 String databaseSql = "create database " + database;
38
39 conn = DriverManager.getConnection(url, username, password);
40 Statement smt = conn.createStatement();
41 if (conn != null) {
42 System.out.println("数据库连接成功!");
43
44 smt.executeUpdate(databaseSql);
45
46 newConn = DriverManager.getConnection(newUrl + database,
47 username, password);
48 if (newConn != null) {
49 System.out.println("已经连接到新创建的数据库:" + database);
50
51 Statement newSmt = newConn.createStatement();
52 int i = newSmt.executeUpdate(tableSql);//DDL语句返回值为0;
53 if (i == 0) {
54 System.out.println(tableSql + "表已经创建成功!");
55 }
56 }
57 }
58
59 } catch (SQLException e1) {
60 // TODO Auto-generated catch block
61 e1.printStackTrace();
62 }
63 return conn;
64 }
65 }
求:用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();
}
}
java连接数据库的代码
用这个类吧.好的话,给我加加分.
import java.sql.*;
/**
* @功能: 一个JDBC的本地化API连接类,封装了数据操作方法,只用传一个SQL语句即可
* @作者: 李开欢
* @日期: 2007/
*/
public class ConnectionDemo {
/*
* 这里可以将常量全部放入另一个类中,以方便修改
*/
private static Connection conn;
private static Statement ps;
private static ResultSet rs;
private static final String DRIVER = "com.microsoft.jdbc.sqlserver.SQLServerDriver";
private static final String URL = "jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=mydb";
private static final String USER ="sa";
private static final String PASS = "sa";
public ConnectionDemo() {
// TODO Auto-generated constructor stub
ConnectionDemo.getConnection();
}
public static Connection getConnection(){
System.out.println("连接中...");
try {
Class.forName(ConnectionDemo.DRIVER);
conn = DriverManager.getConnection(ConnectionDemo.URL, ConnectionDemo.USER, ConnectionDemo.PASS);
System.out.println("成功连接");
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return conn;
}
public static Statement getStatement(String sql){
System.out.println("执行SQL语句中...");
try {
ps = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
if(sql.substring(0, 6).equals("select")){
rs = ps.executeQuery(sql);
System.out.println("执行完查询操作,结果已返回ResultSet集合");
}else if(sql.substring(0, 6).equals("delete")){
ps.executeUpdate(sql);
System.out.println("已执行完毕删除操作");
}else if(sql.substring(0, 6).equals("insert")){
ps.executeUpdate(sql);
System.out.println("已执行完毕增加操作");
}else{
ps.executeUpdate(sql);
System.out.println("已执行完毕更新操作");
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return ps;
}
public static ResultSet getResultSet(){
System.out.println("查询结果为:");
return rs;
}
public static void closeConnection(){
System.out.println("关闭连接中...");
try {
if (rs != null) {
rs.close();
System.out.println("已关闭ResultSet");
}
if (ps != null) {
ps.close();
System.out.println("已关闭Statement");
}
if (conn != null) {
conn.close();
System.out.println("已关闭Connection");
}
} catch (Exception e) {
// TODO: handle exception
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
ConnectionDemo.getConnection();
String sql = "delete from type where id = 1";
ConnectionDemo.getStatement(sql);
String sql2 = "insert into type values(1,'教学设备')";
ConnectionDemo.getStatement(sql2);
String sql1 = "select * from type";
ConnectionDemo.getStatement(sql1);
ResultSet rs = ConnectionDemo.getResultSet();
System.out.println("编号 "+"类 型");
try {
while(rs.next()){
System.out.print(" "+rs.getInt(1)+" ");
System.out.println(rs.getString(2));
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
ConnectionDemo.closeConnection();
}
}
新闻名称:java对数据库操作代码,使用java实现数据库编程第一章上机
URL地址:http://pwwzsj.com/article/heisdh.html