下面是我的代码,我从sqlsever2000中读取数据,读取很正常,数据都可以正常读出,但是我想把数据插入到mysql数据库中的时候,只能够id自动增长,title不能够自动增长,请大家帮忙看看啥原因,在线等,急啊!!!!import java.sql.*;
public class sqltomy
{
//连接mssql数据库
//private String url="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=xuzhoujob";
//private String url="jdbc:sqlserver://localhost:1433;DatabaseName=xuzhoujob";
private String url="jdbc:odbc:xuzhoujob";
//catv是数据库!loacalhost可以用IP和主机
private String user="sa";
private String pwd="123456";
//连接mysql数据库
private String mysqlurl="jdbc:mysql://localhost:3306/mydb?autoReconnect=true";
private String mysqluser="root";
private String mysqlpwd="root";
//获取sqlsever连接
public Connection getcon()
{
Connection con;
try
{ //加载驱动程序
//Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
//创建连接
con=DriverManager.getConnection(url,user,pwd);
return con;
}
catch(ClassNotFoundException e)
{
System.out.println("加载sqlserver驱动程序出错");
}
catch(SQLException e)
{
System.out.println(e.getMessage());
}
catch(Exception e)
{
System.out.println("出现错误了,我也不知道是啥错误,来自sqlsever数据库");
} return null;
}
//连接mysql数据库
public Connection getconmysql()
{
Connection conmysql;
try
{ //加载驱动程序
Class.forName("com.mysql.jdbc.Driver").newInstance();
//创建连接
conmysql=DriverManager.getConnection(mysqlurl,mysqluser,mysqlpwd);
return conmysql;
}
catch(ClassNotFoundException e)
{
System.out.println("加载mysql驱动程序出错");
}
catch(SQLException e)
{
System.out.println(e.getMessage());
}
catch(Exception e)
{
System.out.println("出现错误了,我也不知道是啥错误,来自mysql");
}
return null;
}
public ResultSet executeSql(String sqlstr)
{
Connection conn;
Statement stmt; try
{
conn=getcon();
stmt=conn.createStatement();
ResultSet rs=stmt.executeQuery(sqlstr);
return rs;
}
catch(SQLException e)
{
System.out.print("获取数据集时出现错误");
}
return null;
}
public int inserttomysql(String sql){
Connection conn;
Statement stmt;
try
{
conn=getconmysql();
stmt=conn.createStatement();
int rs=stmt.executeUpdate(sql);
return rs;
}
catch(SQLException e)
{
System.out.print("插入数据集时出现错误");
}
return 1;
}
//插入数据进入mysql
public boolean executeInsert(String insert_SQL) throws Exception {
PreparedStatement pstmt;
pstmt = getconmysql().prepareStatement(insert_SQL);
try {
pstmt.executeUpdate(insert_SQL);
return true;
} catch (Exception ex) { System.err.println(ex.getMessage());
System.out.println("插入不了表哦!");
return false;
}
finally
{
closePreparedStatement();
closeConnection(); }
}
//插入数据后进行更新
public boolean executeUpdate(String update_SQL) throws Exception {
PreparedStatement pstmt;
pstmt =getconmysql().prepareStatement(update_SQL);
try {
pstmt.executeUpdate();
closePreparedStatement();
closeConnection();
return true;
} catch (Exception ex) {
System.err.println("aq.executeQuery: " + ex.getMessage());
closePreparedStatement();
closeConnection();
return false;
}
}
//关闭数据库
public void closeConnection() throws Exception {
Connection conn=getconmysql();
try {
if(conn!= null) {
conn.close();
}
} catch (Exception ex) {
conn.close();
System.out.print(ex.toString());
}
}
//关闭数据库连接
public void closePreparedStatement() throws Exception {
PreparedStatement pstmt = null;
try {
if(pstmt != null) {
pstmt.close();
}
} catch (Exception ex) {
pstmt.close();
System.out.print(ex.toString());
}
}
public static void main(String[] args) throws SQLException
{
ResultSet rs1;
sqltomy sql1=new sqltomy();
rs1=sql1.executeSql("select * from pH_New_Info where NEWid='610'");
try
{
while(rs1.next())
{
String id=rs1.getString("Newid");
String title=rs1.getString("Title");
System.out.println(id);
System.out.println(title);
// String content=rs1.getString("Content");
// String typeid=rs1.getString("Typeid");
// String flag=rs1.getString("Flag");
// String bestnew=rs1.getString("BestNew");
// String hotnew=rs1.getString("HotNew");
// String author=rs1.getString("Author");
// String newform=rs1.getString("NewFrom");
// String time=rs1.getString("DateAndTime");
// System.out.println(time);
//System.out.println("Newid:"+rs1.getString("NewId")+"Title:"+rs1.getString("Title")+"Content:"+rs1.getString("Content"));
String insql="insert into data(title)values(title)";
// String id=rs1.getString("NewId");
// String title=rs1.getString("Title");
sql1.inserttomysql(insql);
}
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
}
}
public class sqltomy
{
//连接mssql数据库
//private String url="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=xuzhoujob";
//private String url="jdbc:sqlserver://localhost:1433;DatabaseName=xuzhoujob";
private String url="jdbc:odbc:xuzhoujob";
//catv是数据库!loacalhost可以用IP和主机
private String user="sa";
private String pwd="123456";
//连接mysql数据库
private String mysqlurl="jdbc:mysql://localhost:3306/mydb?autoReconnect=true";
private String mysqluser="root";
private String mysqlpwd="root";
//获取sqlsever连接
public Connection getcon()
{
Connection con;
try
{ //加载驱动程序
//Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
//创建连接
con=DriverManager.getConnection(url,user,pwd);
return con;
}
catch(ClassNotFoundException e)
{
System.out.println("加载sqlserver驱动程序出错");
}
catch(SQLException e)
{
System.out.println(e.getMessage());
}
catch(Exception e)
{
System.out.println("出现错误了,我也不知道是啥错误,来自sqlsever数据库");
} return null;
}
//连接mysql数据库
public Connection getconmysql()
{
Connection conmysql;
try
{ //加载驱动程序
Class.forName("com.mysql.jdbc.Driver").newInstance();
//创建连接
conmysql=DriverManager.getConnection(mysqlurl,mysqluser,mysqlpwd);
return conmysql;
}
catch(ClassNotFoundException e)
{
System.out.println("加载mysql驱动程序出错");
}
catch(SQLException e)
{
System.out.println(e.getMessage());
}
catch(Exception e)
{
System.out.println("出现错误了,我也不知道是啥错误,来自mysql");
}
return null;
}
public ResultSet executeSql(String sqlstr)
{
Connection conn;
Statement stmt; try
{
conn=getcon();
stmt=conn.createStatement();
ResultSet rs=stmt.executeQuery(sqlstr);
return rs;
}
catch(SQLException e)
{
System.out.print("获取数据集时出现错误");
}
return null;
}
public int inserttomysql(String sql){
Connection conn;
Statement stmt;
try
{
conn=getconmysql();
stmt=conn.createStatement();
int rs=stmt.executeUpdate(sql);
return rs;
}
catch(SQLException e)
{
System.out.print("插入数据集时出现错误");
}
return 1;
}
//插入数据进入mysql
public boolean executeInsert(String insert_SQL) throws Exception {
PreparedStatement pstmt;
pstmt = getconmysql().prepareStatement(insert_SQL);
try {
pstmt.executeUpdate(insert_SQL);
return true;
} catch (Exception ex) { System.err.println(ex.getMessage());
System.out.println("插入不了表哦!");
return false;
}
finally
{
closePreparedStatement();
closeConnection(); }
}
//插入数据后进行更新
public boolean executeUpdate(String update_SQL) throws Exception {
PreparedStatement pstmt;
pstmt =getconmysql().prepareStatement(update_SQL);
try {
pstmt.executeUpdate();
closePreparedStatement();
closeConnection();
return true;
} catch (Exception ex) {
System.err.println("aq.executeQuery: " + ex.getMessage());
closePreparedStatement();
closeConnection();
return false;
}
}
//关闭数据库
public void closeConnection() throws Exception {
Connection conn=getconmysql();
try {
if(conn!= null) {
conn.close();
}
} catch (Exception ex) {
conn.close();
System.out.print(ex.toString());
}
}
//关闭数据库连接
public void closePreparedStatement() throws Exception {
PreparedStatement pstmt = null;
try {
if(pstmt != null) {
pstmt.close();
}
} catch (Exception ex) {
pstmt.close();
System.out.print(ex.toString());
}
}
public static void main(String[] args) throws SQLException
{
ResultSet rs1;
sqltomy sql1=new sqltomy();
rs1=sql1.executeSql("select * from pH_New_Info where NEWid='610'");
try
{
while(rs1.next())
{
String id=rs1.getString("Newid");
String title=rs1.getString("Title");
System.out.println(id);
System.out.println(title);
// String content=rs1.getString("Content");
// String typeid=rs1.getString("Typeid");
// String flag=rs1.getString("Flag");
// String bestnew=rs1.getString("BestNew");
// String hotnew=rs1.getString("HotNew");
// String author=rs1.getString("Author");
// String newform=rs1.getString("NewFrom");
// String time=rs1.getString("DateAndTime");
// System.out.println(time);
//System.out.println("Newid:"+rs1.getString("NewId")+"Title:"+rs1.getString("Title")+"Content:"+rs1.getString("Content"));
String insql="insert into data(title)values(title)";
// String id=rs1.getString("NewId");
// String title=rs1.getString("Title");
sql1.inserttomysql(insql);
}
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
}
}
String insql="insert into data(title)values(title)";
这叫什么sql 出错事肯定的------》 String insql="insert into data(title)values('"+title+"')";
inserttomysql 方法 你的conn和statement都不是静态的 执行完插入后为什么没有关闭操作 如果只是这么简单的功能建议你至少conn做成单例的static变量
String insql="insert into data(title)values(title)";改为:
String insql="insert into data(title)values('" + title + "')";