jsp数据库连上了不能进行插入
packagecom.test;importjava.sql.Connection;importjava.sql.DriverManager;importjava.sql...
package com.test;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public final class JDBconne
{
private static Connection connection = null;
public static Connection getConnetion()
{
String url = "jdbc:sqlserver://localhost:1433;DatabaseName=mybirger";
String user = "sa";
String password = "bingzi";
try
{
DriverManager.registerDriver(new com.microsoft.sqlserver.jdbc.SQLServerDriver());
connection = DriverManager.getConnection(url, user, password);
}
catch (SQLException e)
{
}
return connection;
}
public static void closeConnection()
{
try
{
if(connection != null) connection.close();
}
catch (SQLException e)
{
}
}
}
这是数据库连接用的java
用户操作时的类代码是:
package com.dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
public class UserDao
{
public int insert(Connection connection, User user)
{
int value=-1;
PreparedStatement statement=null;
String sql="insert into user(Username,Password,Name,Sex,Tel,Email)values(?,?,?,?,?,?)";
try
{
statement=connection.prepareStatement(sql);
statement.setString(1,user.getUsername());
statement.setString(2,user.getPassword());
statement.setString(3,user.getName());
statement.setString(4,user.getSex());
statement.setString(5,user.getTel());
statement.setString(6,user.getEmail());
value=statement.executeUpdate();
}
catch (SQLException ex)
{
ex.printStackTrace();
}
finally
{
try
{
if(statement != null) statement.close();
}
catch (SQLException ex)
{
}
}
return value;
} 展开
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public final class JDBconne
{
private static Connection connection = null;
public static Connection getConnetion()
{
String url = "jdbc:sqlserver://localhost:1433;DatabaseName=mybirger";
String user = "sa";
String password = "bingzi";
try
{
DriverManager.registerDriver(new com.microsoft.sqlserver.jdbc.SQLServerDriver());
connection = DriverManager.getConnection(url, user, password);
}
catch (SQLException e)
{
}
return connection;
}
public static void closeConnection()
{
try
{
if(connection != null) connection.close();
}
catch (SQLException e)
{
}
}
}
这是数据库连接用的java
用户操作时的类代码是:
package com.dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
public class UserDao
{
public int insert(Connection connection, User user)
{
int value=-1;
PreparedStatement statement=null;
String sql="insert into user(Username,Password,Name,Sex,Tel,Email)values(?,?,?,?,?,?)";
try
{
statement=connection.prepareStatement(sql);
statement.setString(1,user.getUsername());
statement.setString(2,user.getPassword());
statement.setString(3,user.getName());
statement.setString(4,user.getSex());
statement.setString(5,user.getTel());
statement.setString(6,user.getEmail());
value=statement.executeUpdate();
}
catch (SQLException ex)
{
ex.printStackTrace();
}
finally
{
try
{
if(statement != null) statement.close();
}
catch (SQLException ex)
{
}
}
return value;
} 展开
1个回答
展开全部
这个问题我以前也遇到过(已经解决),具体的解决办法是:依次打开"控制面板"->"管理工具"->"数据源 (ODBC)",选择"系统DSN"->"添加(D)..."->"选择您想为其安装数据源的驱动程序"(以SQL Server为例)->"完成"->"名称"->"描述"->"服务器"->"下一步"(如果你不是很懂的话,接受默认配置)->"下一步"(如果你不是很懂的话,接受默认配置)->"完成"(如果你不是很懂的话,接受默认配置)。到此为止,你的问题已经解决了,快去看看能不能连接了。
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询