求:用Java连接数据库和简单的数据库操作代码
数据库是SQL2005如果告诉我,我可以把我所有的分都给你,真的很需要对数据库的增删查改。需要加2005的驱动程序...
数据库是SQL2005
如果告诉我,我可以把我所有的分都给你,真的很需要
对数据库的增删查改。需要加2005的驱动程序 展开
如果告诉我,我可以把我所有的分都给你,真的很需要
对数据库的增删查改。需要加2005的驱动程序 展开
5个回答
展开全部
以上的代码都不如哥的 且看哥是怎么写条理清晰的代码的!!!
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();
}
}
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();
}
}
展开全部
package common;
import java.sql.*;
public class DBConnect {
private Connection conn = null;
private Statement stmt = null;
private ResultSet rs = null;
private PreparedStatement ps = null;
public DBConnect()
{
try
{
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
conn = DriverManager.getConnection("jdbc:sqlserver://localhost:1433;databasename=HcitPos", "sa", "123456");
stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
}
catch (SQLException ex)
{
System.out.println(ex.getMessage() + "路径错误");
}
catch (ClassNotFoundException ex)
{
System.out.println(ex.getMessage() + "驱动错误");
}
}
public PreparedStatement getPs(String sql) throws SQLException {
try {
ps = conn.prepareStatement(sql);
conn.commit();
return ps;
} catch (Exception e) {
//conn.rollback();
e.printStackTrace();
return ps;
}
}
public ResultSet executeQuery(String ssql) throws SQLException{
try{
rs = stmt.executeQuery(ssql);
return rs;
}
catch(SQLException se){
//conn.rollback();
System.out.println("DBBean.executeQuery() ERROR:"+se.getMessage());
}
return rs;
}
public int executeUpdate(String ssql) throws SQLException{
int iupdate = 0;
try{
iupdate = stmt.executeUpdate(ssql);
return iupdate;
}
catch(SQLException se){
//conn.rollback();
System.out.println("DBBean.executeUpdate() ERROR:"+se.getMessage());
}
return iupdate;
}
public void free() throws SQLException{
try{
if(rs != null) rs.close();
if(stmt != null) stmt.close();
if(conn != null) conn.close();
}
catch(SQLException se){
System.out.println("DBBean.free() ERROR:"+se.getMessage());
}
}
}
这里用的是直连,要导入一下驱动包就可用了
import java.sql.*;
public class DBConnect {
private Connection conn = null;
private Statement stmt = null;
private ResultSet rs = null;
private PreparedStatement ps = null;
public DBConnect()
{
try
{
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
conn = DriverManager.getConnection("jdbc:sqlserver://localhost:1433;databasename=HcitPos", "sa", "123456");
stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
}
catch (SQLException ex)
{
System.out.println(ex.getMessage() + "路径错误");
}
catch (ClassNotFoundException ex)
{
System.out.println(ex.getMessage() + "驱动错误");
}
}
public PreparedStatement getPs(String sql) throws SQLException {
try {
ps = conn.prepareStatement(sql);
conn.commit();
return ps;
} catch (Exception e) {
//conn.rollback();
e.printStackTrace();
return ps;
}
}
public ResultSet executeQuery(String ssql) throws SQLException{
try{
rs = stmt.executeQuery(ssql);
return rs;
}
catch(SQLException se){
//conn.rollback();
System.out.println("DBBean.executeQuery() ERROR:"+se.getMessage());
}
return rs;
}
public int executeUpdate(String ssql) throws SQLException{
int iupdate = 0;
try{
iupdate = stmt.executeUpdate(ssql);
return iupdate;
}
catch(SQLException se){
//conn.rollback();
System.out.println("DBBean.executeUpdate() ERROR:"+se.getMessage());
}
return iupdate;
}
public void free() throws SQLException{
try{
if(rs != null) rs.close();
if(stmt != null) stmt.close();
if(conn != null) conn.close();
}
catch(SQLException se){
System.out.println("DBBean.free() ERROR:"+se.getMessage());
}
}
}
这里用的是直连,要导入一下驱动包就可用了
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
一楼的是橡慧桥连 我这是jdbc直连 需轮如晌要加2005的驱动程序
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
Connection connection =DriverManager.getConnection(url,username,password);
Statement statement=connection.createStatement();
ResultSet resultset=statement.executeQuery("select * from students ");
while(resultset.next()){
String admin=resultset.getString("admin");
String name=resultset.getString("腊锋name");
String age=resultset.getString("age");
String grade=resultset.getString("grade");
String magor=resultset.getString("magor");
String dept=resultset.getString("dept");
}
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
Connection connection =DriverManager.getConnection(url,username,password);
Statement statement=connection.createStatement();
ResultSet resultset=statement.executeQuery("select * from students ");
while(resultset.next()){
String admin=resultset.getString("admin");
String name=resultset.getString("腊锋name");
String age=resultset.getString("age");
String grade=resultset.getString("grade");
String magor=resultset.getString("magor");
String dept=resultset.getString("dept");
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
public class Example
{
public static void main(String[] args)throws
SQLExcetion,ClassNotFoundExcetion
{
Class.forName("sun.jdbc.odbc.jdbcOdbcDrice");
Connetion con=DriverManager.getConnection ("jdbc:odbc:javatest","","");
Statement stmt=con.createStatement();
String sql="冲空create table math(整数键指 int,平方 int,平方根 float,正弦散亮瞎 float)";
stmt.executeUpdate(sql);
}
}
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
public class Example
{
public static void main(String[] args)throws
SQLExcetion,ClassNotFoundExcetion
{
Class.forName("sun.jdbc.odbc.jdbcOdbcDrice");
Connetion con=DriverManager.getConnection ("jdbc:odbc:javatest","","");
Statement stmt=con.createStatement();
String sql="冲空create table math(整数键指 int,平方 int,平方根 float,正弦散亮瞎 float)";
stmt.executeUpdate(sql);
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
最近项目芹岁举写这个写的嫌碧快吐血了。。。随便看了下
啦啦·爱琴海 这个写的和雀备我的查不多。。。
其实也就那么几条语句。
啦啦·爱琴海 这个写的和雀备我的查不多。。。
其实也就那么几条语句。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询