Myeclipse和Mysql连接后,怎么写java语句改变数据库表的内容
我连接好后,怎么写java代码啊,比如说我已经写好一个登录窗口,输入用户名和密码,接下来我怎么把接收到的用户名和密码在数据库里面找到验证然后跳转到下一个窗口我才自学这里,...
我连接好后,怎么写java代码啊,比如说我已经写好一个登录窗口,输入用户名和密码,接下来我怎么把接收到的用户名和密码在数据库里面找到验证然后跳转到下一个窗口
我才自学这里,希望大家帮帮我。我没有财富值,都送出去了 55 展开
我才自学这里,希望大家帮帮我。我没有财富值,都送出去了 55 展开
3个回答
展开全部
注册JSP:
<body>
<form action="regist" method="post">
用户名:<input type="text" name="userName"/><br/>
密码:<input type="text" name="password"/><br/>
<input type="submit">
</form>
</body>
servlet类<注:需在WEB.XML中配置servlet>:
public class RegisServlet extends HttpServlet {
private String driverClass=null;
private String url=null;
private String dbUserName=null;
private String dbPassword=null;
protected void service(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
String name=request.getParameter("userName");
String password=request.getParameter("password");
Connection conn = null;
PreparedStatement psta=null;
try {
conn = DriverManager.getConnection(url, dbUserName, dbPassword);
conn.setAutoCommit(false);
String sql = " insert into user(name,password) values (?,?)";
psta = conn.prepareStatement(sql);
psta.setString(1, name.trim());
psta.setString(2, password.trim());
psta.execute();
conn.commit();
} catch (SQLException e) {
try {
conn.rollback();
} catch (SQLException e1) {
e1.printStackTrace();
}
e.printStackTrace();
} finally {
if(conn!=null){
try {
conn.close();
} catch (SQLException e) {
}
}
if(psta!=null){
try {
psta.close();
} catch (SQLException e) {
}
}
}
//浏览器回应
PrintWriter out = response.getWriter();
StringBuffer strb = new StringBuffer();
strb.append("<html>").append("<head>").append("<title>").append("用户列表")
.append("</title>").append("</head>").append("<body>");
strb.append("<form action='login' method='post'> " +
"用户名:<input type='text' name='userName'/><br/> " +
"密码:<input type='text' name='password'/><br/> " +
"<input type='submit'> " +
"</form>");
strb.append("</body>");
strb.append("</html>");
response.setCharacterEncoding("UTF-8");
response.setContentType("text/html;charset=UTF-8");
out.print(strb.toString());
out.flush();
out.close();
}
public void init() throws ServletException {
InputStream is = RegisServlet.class.getClassLoader()
.getResourceAsStream("com/jdbc.properties");
Properties p = new Properties();
try {
p.load(is);
} catch (IOException e) {
e.printStackTrace();
}
this.driverClass = p.getProperty("driverClass").trim();
this.url = p.getProperty("url").trim();
this.dbUserName = p.getProperty("userName").trim();
this.dbPassword = p.getProperty("password").trim();
try {
Class.forName(driverClass);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
}
mysql数据库连接类:
public class TestJdbc {
public static void main(String[] args) {
InputStream is = RegisServlet.class.getClassLoader()
.getResourceAsStream("com/jdbc.properties");
Properties p = new Properties();
try {
p.load(is);
} catch (IOException e) {
e.printStackTrace();
}
String driverClass = p.getProperty("driverClass").trim();
String url = p.getProperty("url").trim();
String userName = p.getProperty("userName").trim();
String password = p.getProperty("password").trim();
try {
Class.forName(driverClass);
Connection conn =DriverManager.getConnection(url,userName,password);
System.out.println(conn);
} catch (Exception e) {
e.printStackTrace();
}
}
}
JDBC.properties文件:
driverClass=com.mysql.jdbc.Driver
url=jdbc:mysql://127.0.0.1:3306/userdb
userName=root
password=root
<body>
<form action="regist" method="post">
用户名:<input type="text" name="userName"/><br/>
密码:<input type="text" name="password"/><br/>
<input type="submit">
</form>
</body>
servlet类<注:需在WEB.XML中配置servlet>:
public class RegisServlet extends HttpServlet {
private String driverClass=null;
private String url=null;
private String dbUserName=null;
private String dbPassword=null;
protected void service(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
String name=request.getParameter("userName");
String password=request.getParameter("password");
Connection conn = null;
PreparedStatement psta=null;
try {
conn = DriverManager.getConnection(url, dbUserName, dbPassword);
conn.setAutoCommit(false);
String sql = " insert into user(name,password) values (?,?)";
psta = conn.prepareStatement(sql);
psta.setString(1, name.trim());
psta.setString(2, password.trim());
psta.execute();
conn.commit();
} catch (SQLException e) {
try {
conn.rollback();
} catch (SQLException e1) {
e1.printStackTrace();
}
e.printStackTrace();
} finally {
if(conn!=null){
try {
conn.close();
} catch (SQLException e) {
}
}
if(psta!=null){
try {
psta.close();
} catch (SQLException e) {
}
}
}
//浏览器回应
PrintWriter out = response.getWriter();
StringBuffer strb = new StringBuffer();
strb.append("<html>").append("<head>").append("<title>").append("用户列表")
.append("</title>").append("</head>").append("<body>");
strb.append("<form action='login' method='post'> " +
"用户名:<input type='text' name='userName'/><br/> " +
"密码:<input type='text' name='password'/><br/> " +
"<input type='submit'> " +
"</form>");
strb.append("</body>");
strb.append("</html>");
response.setCharacterEncoding("UTF-8");
response.setContentType("text/html;charset=UTF-8");
out.print(strb.toString());
out.flush();
out.close();
}
public void init() throws ServletException {
InputStream is = RegisServlet.class.getClassLoader()
.getResourceAsStream("com/jdbc.properties");
Properties p = new Properties();
try {
p.load(is);
} catch (IOException e) {
e.printStackTrace();
}
this.driverClass = p.getProperty("driverClass").trim();
this.url = p.getProperty("url").trim();
this.dbUserName = p.getProperty("userName").trim();
this.dbPassword = p.getProperty("password").trim();
try {
Class.forName(driverClass);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
}
mysql数据库连接类:
public class TestJdbc {
public static void main(String[] args) {
InputStream is = RegisServlet.class.getClassLoader()
.getResourceAsStream("com/jdbc.properties");
Properties p = new Properties();
try {
p.load(is);
} catch (IOException e) {
e.printStackTrace();
}
String driverClass = p.getProperty("driverClass").trim();
String url = p.getProperty("url").trim();
String userName = p.getProperty("userName").trim();
String password = p.getProperty("password").trim();
try {
Class.forName(driverClass);
Connection conn =DriverManager.getConnection(url,userName,password);
System.out.println(conn);
} catch (Exception e) {
e.printStackTrace();
}
}
}
JDBC.properties文件:
driverClass=com.mysql.jdbc.Driver
url=jdbc:mysql://127.0.0.1:3306/userdb
userName=root
password=root
展开全部
建个javaweb项目,然后在src下新建java类,然后在写java程序 连接数据库 等
再建个servlet然后在web.xml中配置,在servlet中调用java类 然后发布 启动tomcat 运行 就ok
再建个servlet然后在web.xml中配置,在servlet中调用java类 然后发布 启动tomcat 运行 就ok
追问
我不用web 就是cs 比如说小程序呢
追答
那就见java项目 写个java类 在main函数中运行 照样可以 程序你学着写
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
用连接数据库的类去直接操作就是了
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询