如何用Java向数据库中添加数据?
2个回答
展开全部
1.提取单条记录
//import java.sql.*;
Connection conn=null;
Statement stmt=null;
ResultSet rs=null;
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
String url="jdbc:odbc:%%1";
con=DriverManager.getConnection(url,%%2,%%3);
stmt=conn.createStatement();
stmt.executeUpdate(%%4);
rs=stmt.executeQuery(%%5);
}catch(Exception e){
e.printStackTrace();
}
finally{
try {
if(rs!=null)
rs.close();
if(stmt!=null)
stmt.close();
if(conn!=null)
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
3.显示表格
/*
import java.awt.*;
import javax.swing.*;
import java.sql.*;
import javax.swing.table.*;
*/
String[] colHeads=%%4;
Connection conn=null;
Statement stmt=null;
ResultSet rs=null;
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
String url="jdbc:odbc:%%1";
conn=DriverManager.getConnection(url,%%2,%%3);
stmt=conn.createStatement();
rs=stmt.executeQuery("SELECT count(*) as au_count from "+%%5);
rs.next();
int iCount=rs.getInt("au_count");
Object[][] data=new Object[iCount][];
int i=0;
rs=stmt.executeQuery("SELECT * from "+%%5);
while(rs.next()){
data[i]=new Object[iCount];
data[i][0]=rs.getString("au_fname");
data[i][1]=rs.getString("Phone");
data[i][2]=rs.getString("City");
i++;
}
JTable table=new JTable(data,colHeads);
JScrollPane jsp=new JScrollPane(table);
getContentPane().add(jsp);
}catch(Exception e){
e.printStackTrace();
}
finally{
try {
if(rs!=null)
rs.close();
if(stmt!=null)
stmt.close();
if(conn!=null)
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
6.关闭时关闭连接
//import java.sql.*;
addWindowListener(new WindowAdapter{
public void windowClosing(WindowEvent wevent){
if(stmt!=null){
try {
if(rs!=null)
rs.close();
if(stmt!=null)
stmt.close();
if(conn!=null)
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
7.执行命令
//import java.sql.*;
Connection conn=null;
PreparedStatement pst=null;
try {
conn=DriverManager.getConnection(url);
pst=conn.prepareStatement("Insert Into grade(%%1) Values (?)");
pst.setInt(1,%%2);
//pst.setString(2,%%2);
pst.addBatch();
pst.executeBatch();
} catch (SQLException e){
e.printStackTrace();
}
finally{
try {
if (pst != null)
pst.close();
if (conn != null)
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//import java.sql.*;
Connection conn=null;
Statement stmt=null;
ResultSet rs=null;
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
String url="jdbc:odbc:%%1";
con=DriverManager.getConnection(url,%%2,%%3);
stmt=conn.createStatement();
stmt.executeUpdate(%%4);
rs=stmt.executeQuery(%%5);
}catch(Exception e){
e.printStackTrace();
}
finally{
try {
if(rs!=null)
rs.close();
if(stmt!=null)
stmt.close();
if(conn!=null)
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
3.显示表格
/*
import java.awt.*;
import javax.swing.*;
import java.sql.*;
import javax.swing.table.*;
*/
String[] colHeads=%%4;
Connection conn=null;
Statement stmt=null;
ResultSet rs=null;
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
String url="jdbc:odbc:%%1";
conn=DriverManager.getConnection(url,%%2,%%3);
stmt=conn.createStatement();
rs=stmt.executeQuery("SELECT count(*) as au_count from "+%%5);
rs.next();
int iCount=rs.getInt("au_count");
Object[][] data=new Object[iCount][];
int i=0;
rs=stmt.executeQuery("SELECT * from "+%%5);
while(rs.next()){
data[i]=new Object[iCount];
data[i][0]=rs.getString("au_fname");
data[i][1]=rs.getString("Phone");
data[i][2]=rs.getString("City");
i++;
}
JTable table=new JTable(data,colHeads);
JScrollPane jsp=new JScrollPane(table);
getContentPane().add(jsp);
}catch(Exception e){
e.printStackTrace();
}
finally{
try {
if(rs!=null)
rs.close();
if(stmt!=null)
stmt.close();
if(conn!=null)
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
6.关闭时关闭连接
//import java.sql.*;
addWindowListener(new WindowAdapter{
public void windowClosing(WindowEvent wevent){
if(stmt!=null){
try {
if(rs!=null)
rs.close();
if(stmt!=null)
stmt.close();
if(conn!=null)
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
7.执行命令
//import java.sql.*;
Connection conn=null;
PreparedStatement pst=null;
try {
conn=DriverManager.getConnection(url);
pst=conn.prepareStatement("Insert Into grade(%%1) Values (?)");
pst.setInt(1,%%2);
//pst.setString(2,%%2);
pst.addBatch();
pst.executeBatch();
} catch (SQLException e){
e.printStackTrace();
}
finally{
try {
if (pst != null)
pst.close();
if (conn != null)
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
展开全部
假设已经连接上数据库则有下面这两个变量 Connection connect //连接到数据源 Statement statement=connect.createStatement(); 使用语句 String sql="INSERT INTO TABLENAME(Username,password) VALUES('"+name+"','"+pass+"')"; //TABLENAME是数据库中的表名 statement.executeUpdate(sql);
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询