下面是我的代码,我从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());
}
}
}
解决方案 »
- 关于 ibatis 的配置问题
- js操作
- tomcat5.5 ssi 中文乱码
- 奖50分问大家:一般怎样对java产品进行加密,用加密狗还是其它的什么方式??帮帮我!!
- STRUTS html:checkbox问题
- 有没有免费的jsp debug和javascript debug的eclipse插件?告知名称,下载地址马上给100分!
- weblogic8.1出现的一段错误,请高手来帮忙,成功再加50分!
- 如何判断编辑框中输入的是int而不是其他字符,除了抛出异常还有其他方法吗(在线等待,多谢!)
- 向高手求救,要如何获得pdf文件包含多少页
- MultipartHttpServletRequest 如何接收表单参数
- 使用<bean:message>国际化的问题
- ewebeditor怎么去掉硬回车
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 + "')";