当前位置: 技术问答>java相关
用java怎样操作数据库?
来源: 互联网 发布时间:2015-02-01
本文导语: import java.sql.*; public class createauthtab { public static void main(String args[]) { try { Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); } catch(java.lang.ClassNotFoundException e) { System.out.println(e.getMessage()); } try { Connection con=Dr...
import java.sql.*;
public class createauthtab
{
public static void main(String args[])
{
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
}
catch(java.lang.ClassNotFoundException e)
{
System.out.println(e.getMessage());
}
try
{
Connection con=DriverManager.getConnection("jdbc:odbc:sunshine","sa","sunshine");
Statement stmt=con.createStatement();
stmt.executeUpdate("create table authtab("+"auth char(10),"+
"tel char(12) CONSTRAINT authlndex PRIMARY KEY)");
stmt.close();
con.close();
}
catch(SQLException ex)
{
System.out.println("SQLException:"+ex.getMessage());
}
}
}
public class createauthtab
{
public static void main(String args[])
{
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
}
catch(java.lang.ClassNotFoundException e)
{
System.out.println(e.getMessage());
}
try
{
Connection con=DriverManager.getConnection("jdbc:odbc:sunshine","sa","sunshine");
Statement stmt=con.createStatement();
stmt.executeUpdate("create table authtab("+"auth char(10),"+
"tel char(12) CONSTRAINT authlndex PRIMARY KEY)");
stmt.close();
con.close();
}
catch(SQLException ex)
{
System.out.println("SQLException:"+ex.getMessage());
}
}
}
|
sql = "create table authtab (auth char(10), tel char(12) CONSTARAINT authlndex PRIMARY KEY)";
stmt.executeUpdate(sql);
stmt.executeUpdate(sql);
|
你到底出了什么问题?不能建表?
建表之前先删除该表,或者监测该表是否存在,如果存在就drop后create.
建表之前先删除该表,或者监测该表是否存在,如果存在就drop后create.