本文共 3050 字,大约阅读时间需要 10 分钟。
程序如下//加载数据库的驱动packagemainmethod;importjava.io.IOException;importjava.sql.*;importjava.io.*;publicclassmainmethod{privatestaticConnectionstate;staticConnection...
程序如下
//加载数据库的驱动
package mainmethod;
import java.io.IOException;
import java.sql.*;
import java.io.*;
public class mainmethod{
private static Connection state;
static Connection conn=null;
static{
try{
Class.forName("net.sourceforge.jtds.jdbc.Driver");
}catch(ClassNotFoundException e){
e.printStackTrace();
}
}
public static Connection getConn(){
String url="jdbc:jtds:sqlserver://127.0.0.1:1433/S_C";
String userName="sa";
String passWord="";
try{
conn=DriverManager.getConnection(url, userName, passWord);
if(conn!=null){
System.out.println("ok!!!!!!!");
}
}
catch(SQLException e){
e.printStackTrace();
}
System.out.println(conn);//苦思冥想没有结局的问题(不能执行sql语句)解决方法,原因是conn的值有没有赋值上去,或者被顶替掉。
return conn;
}
@SuppressWarnings("deprecation")
public static class input{
static int inputIntdate() throws IOException{ //这里之所以不使用public是因为在input类前的public对类里面的内容有影响
byte buf[]=new byte[5];
String str;
int num = 0;
try{
System.in.read(buf);
str=new String(buf,0);
num=Integer.parseInt(str.trim());
}catch(Exception e){
System.out.println(e);
}
return num;
}
}
//添加功能
static class Insert{
public static Insert insert() throws SQLException{
getConn();
String sql="";
Statement state=conn.createStatement();
sql="insert into Student values('2010109','李雷','男 ','计算机系','IT','本科',266)";
state.executeUpdate(sql);
System.out.println("insert is ok!");
state.close();
return null;
}
}
public static void delete() throws SQLException{
getConn();
}
public static void update() throws SQLException{
getConn();
}
public static void select() throws SQLException{
getConn();
}
public static void main(String[] args) throws SQLException, IOException{
System.out.println ("分别输入数字1,2,3,4选择增,删,改,查模块");
//需要在这里添加输入整数n的模块
int m;
m=input.inputIntdate();
switch(m){
case 1:
System.out.println("ok");
Insert in =null;
in=Insert.insert();
break;
case 2:System.out.println("ok");delete();break;
case 3:System.out.println("ok");update();break;
case 4:System.out.println("ok");select();break;
default:System.out.println("输入的整数有误");
}
//需要添加重新选侧模块
}
}
没有向Student表中插入任何数据
运行的结果是这样的
分别输入数字1,2,3,4选择增,删,改,查模块
1
ok
ok!!!!!!!
net.sourceforge.jtds.jdbc.JtdsConnection@188f506
Exception in thread "main" java.sql.DataTruncation: Data truncation
at net.sourceforge.jtds.jdbc.SQLDiagnostic.addDiagnostic(SQLDiagnostic.java:386)
at net.sourceforge.jtds.jdbc.TdsCore.tdsErrorToken(TdsCore.java:2893)
at net.sourceforge.jtds.jdbc.TdsCore.nextToken(TdsCore.java:2335)
at net.sourceforge.jtds.jdbc.TdsCore.getMoreResults(TdsCore.java:638)
at net.sourceforge.jtds.jdbc.JtdsStatement.processResults(JtdsStatement.java:613)
at net.sourceforge.jtds.jdbc.JtdsStatement.executeSQL(JtdsStatement.java:572)
at net.sourceforge.jtds.jdbc.JtdsStatement.executeImpl(JtdsStatement.java:809)
at net.sourceforge.jtds.jdbc.JtdsStatement.executeUpdate(JtdsStatement.java:1288)
at net.sourceforge.jtds.jdbc.JtdsStatement.executeUpdate(JtdsStatement.java:1241)
展开
转载地址:https://blog.csdn.net/weixin_32601635/article/details/114252685 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!